note 107537 added to function.getimagesize
| From: | alexyam at live dot com | Date: | Wed, 15 Feb 2012 23:30:13 +0000 |
| Subject: | note 107537 added to function.getimagesize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186160@lists.php.net to get a copy of this message | ||
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
/*
----------------------------------------------------------------------
PHP Blob Data As File Stream v1.0 (C) 2012 Alex Yam <alexyam@live.com>
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"
}
*/
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);
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 202.55.35.60
----
Manual Page -- http://www.php.net/manual/en/function.getimagesize.php
Edit -- https://master.php.net/note/edit/107537
Del: integrated -- https://master.php.net/note/delete/107537/integrated
Del: useless -- https://master.php.net/note/delete/107537/useless
Del: bad code -- https://master.php.net/note/delete/107537/bad+code
Del: spam -- https://master.php.net/note/delete/107537/spam
Del: non-english -- https://master.php.net/note/delete/107537/non-english
Del: in docs -- https://master.php.net/note/delete/107537/in+docs
Del: other reasons-- https://master.php.net/note/delete/107537
Reject -- https://master.php.net/note/reject/107537
Search -- https://master.php.net/manage/user-notes.php