php<5.4 getimagesizefromstring
class getImgStream{
private $imgStream;
private $position;
function stream_open($path, $mode, $options, &$opened_path){
$url = parse_url($path);
$this->imgStream = $GLOBALS[$url["host"]];
$this->position = 0;
return true;
}
function stream_read($count){
$ret = substr($this->imgStream, $this->position, $count);
$this->position += strlen($ret);
return $ret;
}
function stream_stat(){
//maxmemory: 5 * 1024 * 1024;
$fp = fopen("php://temp/maxmemory:5242880", 'r+');
fwrite($fp, $this->imgStream);
$fstat = fstat($fp);
fclose($fp);
return $fstat;
}
function stream_eof(){
return $this->position >= strlen($this->imgStream);
}
function stream_tell(){
return $this->position;
}
function stream_close(){
unset($this->imgStream, $this->position);
}
}
$file_path = 'http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif';
$stream = file_get_contents($file_path);
stream_wrapper_register("image", "getImgStream");
print_r(getimagesize('image://stream'));
$new_img = imagecreatefromgif('image://stream');//或者$new_img = imagecreatefromstring($stream);
print_r($new_img);
imagejpeg($new_img, 'E:\WEB\uploads\test.jpg', 100);
getimagesizefromstring
(PHP 5 >= 5.4.0)
getimagesizefromstring — 画像の大きさを文字列から取得する
説明
array getimagesizefromstring
( string
$imagedata
[, array &$imageinfo
] )getimagesize() と似ていますが、 getimagesizefromstring() の最初のパラメータはファイル名ではなく文字列となります。
この関数の挙動の詳細については getimagesize() のドキュメントを参照ください。
パラメータ
-
imagedata -
文字列で表した画像データ。
-
imageinfo -
getimagesize() を参照ください。
返り値
getimagesize() を参照ください。
例
例1 getimagesizefromstring() の例
<?php
$img = '/path/to/test.png';
// ファイルとして開く
$size_info1 = getimagesize($img);
// あるいは、文字列として開く
$data = file_get_contents($img);
$size_info2 = getimagesizefromstring($data);
?>
jxwangchao at gmail dot com ¶
1 month ago
