getcoo php_PHP: getimagesize - Manual

I wanted to use getimagesize() on .SWF files stored in the database as blob data and couldn't find a simple solution, so I created my own.

I am releasing this code under the MIT license to save everyone some time:

----------------------------------------------------------------------

PHP Blob Data As File Stream v1.0 (C) 2012 Alex Yam

This code is released under the MIT License.

----------------------------------------------------------------------

[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.

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"

}

*/classblob_data_as_file_stream{

private static$blob_data_position=0;

public static$blob_data_stream='';

public static functionstream_open($path,$mode,$options,&$opened_path){

static::$blob_data_position=0;

returntrue;

}

public static functionstream_seek($seek_offset,$seek_whence){$blob_data_length=strlen(static::$blob_data_stream);

switch ($seek_whence) {

caseSEEK_SET:$new_blob_data_position=$seek_offset;

break;

caseSEEK_CUR:$new_blob_data_position= static::$blob_data_position+$seek_offset;

break;

caseSEEK_END:$new_blob_data_position=$blob_data_length+$seek_offset;

break;

default:

returnfalse;

}

if (($new_blob_data_position>=0) AND ($new_blob_data_position<=$blob_data_length)){

static::$blob_data_position=$new_blob_data_position;

returntrue;

}else{

returnfalse;

}

}

public static functionstream_tell(){

return static::$blob_data_position;

}

public static functionstream_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 functionstream_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 functionstream_eof(){

return static::$blob_data_position>=strlen(static::$blob_data_stream);

}

}?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值