2010-12-21 103 views
12

我想加载一个SWF文件并动态确定其高度和宽度。有没有人知道可以使用PHP读取swf文件的维度?PHP获取SWF文件的尺寸

这里的人谁需要什么工作吧:

$aInfo = getimagesize($sFile); 
list($iWidth, $iHeight) = $aInfo; 
+0

你的意思是你需要动态的H,W? – Sudantha 2010-12-21 05:32:26

回答

26

使用getimagesize

例如:

php -r "print_r(getimagesize('http://adsatt.espn.go.com/ad/sponsors/ibm/Oct_2010/ibm0-728x90-0160.swf'));" 
 
Array 
(
    [0] => 728 
    [1] => 90 
    [2] => 13 
    [3] => width="728" height="90" 
    [mime] => application/x-shockwave-flash 
) 

PS:module gd is required

+0

是的,我只是偶然发现了getimagesize(),这正是我需要的。 – tmartin314 2010-12-21 05:40:31

+9

我不知道getimagesize与swf文件一起工作。 – Srisa 2010-12-21 06:47:45

+0

感谢您的回答! – Marty 2011-07-26 01:21:03

2

对于那些寻找存储在数据库中的.swf文件上getimagesize()的解决方案, 这里是我写的stream_wrapper。经过PHP 5.3.10测试。

<?php 
/* 
     ======================================================================= 
     PHP Blob Data As File Stream v1.0 
     ======================================================================= 
     This code is released under the MIT License. 
     (http://www.opensource.org/licenses/MIT) 

     Copyright (C) 2012 Alex Yam <[email protected]> 

     Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 

     The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 

     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 

     ======================================================================= 
     Code Summary 
     ======================================================================= 
     A simple class for PHP functions to read and write blob data as a file 
     using a stream wrapper. 

     Particularly useful for running getimagesize() to get the width and 
     height of .SWF Flash files that are stored in the database as blob data. 

     Requests for a simple way to get the width and height of .SWF blob data 
     in PHP have been around for years. So I decided to write and release 
     this code under the MIT license to save everyone some time, hopefully 
     this will also benefit people in other use cases. 

     Tested on PHP 5.3.10. 

     ======================================================================= 
     Usage Example 
     ======================================================================= 
       #Include 
         include('./blob_data_as_file_stream.php'); 

       #Register the stream wrapper 
         stream_wrapper_register("BlobDataAsFileStream", "blob_data_as_file_stream"); 

       #Fetch a .SWF file from the Adobe website and store it into a variable. 
       #Replace this with your own fetch-swf-blob-data-from-database code. 
         $swf_url = 'http://www.adobe.com/swf/software/flash/about/flashAbout_info_small.swf'; 
         $swf_blob_data = file_get_contents($swf_url); 

       #Store $swf_blob_data to the data stream 
         blob_data_as_file_stream::$blob_data_stream = $swf_blob_data; 

       #Run getimagesize() on the data stream 
         $swf_info = getimagesize('BlobDataAsFileStream://'); 
         var_dump($swf_info); 

     ======================================================================= 
     Usage Output 
     ======================================================================= 
       array(5) { 
        [0]=> 
        int(159) 
        [1]=> 
        int(91) 
        [2]=> 
        int(13) 
        [3]=> 
        string(23) "width="159" height="91"" 
        ["mime"]=> 
        string(29) "application/x-shockwave-flash" 
       } 

     ======================================================================= 
     Note for PHP version < 5.3.0 
     ======================================================================= 
     Replace all 'static::' with 'self::' 
     Reference: http://php.net/manual/en/language.oop5.late-static-bindings.php 

*/ 

class blob_data_as_file_stream { 

     private static $blob_data_position = 0; 
     public static $blob_data_stream = ''; 

     public static function stream_open($path,$mode,$options,&$opened_path){ 
       static::$blob_data_position = 0; 
       return true; 
     } 

     public static function stream_seek($seek_offset,$seek_whence){ 
       $blob_data_length = strlen(static::$blob_data_stream); 
       switch ($seek_whence) { 
         case SEEK_SET: 
           $new_blob_data_position = $seek_offset; 
           break; 
         case SEEK_CUR: 
           $new_blob_data_position = static::$blob_data_position+$seek_offset; 
           break; 
         case SEEK_END: 
           $new_blob_data_position = $blob_data_length+$seek_offset; 
           break; 
         default: 
           return false; 
       } 
       if (($new_blob_data_position >= 0) AND ($new_blob_data_position <= $blob_data_length)){ 
         static::$blob_data_position = $new_blob_data_position; 
         return true; 
       }else{ 
         return false; 
       } 
     } 

     public static function stream_tell(){ 
       return static::$blob_data_position; 
     } 

     public static function stream_read($read_buffer_size){ 
       $read_data = substr(static::$blob_data_stream,static::$blob_data_position,$read_buffer_size); 
       static::$blob_data_position += strlen($read_data); 
       return $read_data; 
     } 

     public static function stream_write($write_data){ 
       $write_data_length=strlen($write_data); 
       static::$blob_data_stream =   substr(static::$blob_data_stream,0,static::$blob_data_position).$write_data.substr(static::$blob_data_stream,static::$blob_data_position+=$write_data_length); 
       return $write_data_length; 
     } 

     public static function stream_eof(){ 
       return static::$blob_data_position >= strlen(static::$blob_data_stream); 
     } 

} 
?>