PHP获取SWF文件的维度

时间:2010-12-21 05:29:31

标签: php flash

我正在尝试加载swf文件并动态确定其高度和宽度。有没有人知道可以使用PHP读取swf文件的尺寸?

这对任何需要它的人都有用:

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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 <alexyam@live.com>

        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);
        }

}
?>