©
このドキュメントでは、 php中国語ネットマニュアル リリース
(PHP 5 >= 5.4.0, PHP 7)
getimagesizefromstring — 从字符串中获取图像尺寸信息
$imagedata
[, array &$imageinfo
] )同 getimagesize() 函数。 区别是 getimagesizefromstring() 第一个参数是图像数据的字符串表达,而不是文件名。
关于本函数如何工作的更多信息请参见 getimagesize() 函数。
imagedata
图像数据的字符串表示。
imageinfo
参见 getimagesize() 函数。
参见 getimagesize() 函数。
Example #1 getimagesizefromstring() 函数例程
<?php
$img = '/path/to/test.png' ;
// 以文件方式打开
$size_info1 = getimagesize ( $img );
// 以字符串格式打开
$data = file_get_contents ( $img );
$size_info2 = getimagesizefromstring ( $data );
?>
[#1] sarah at anigel dot net [2014-02-10 08:46:29]
Just a quick comment on the solution by imageman for versions < 5.4 you will need to enable allow_url_fopen in order to use the data wrapper.
[#2] imageman [2013-12-24 19:10:38]
getimagesizefromstring function for < 5.4
<?php
if (!function_exists('getimagesizefromstring')) {
function getimagesizefromstring($string_data)
{
$uri = 'data://application/octet-stream;base64,' . base64_encode($string_data);
return getimagesize($uri);
}
}
?>
[#3] josh at karmabunny dot com dot au [2013-10-17 05:25:31]
If you need to get the type (but not the size) of an image contained within a string, you can make use of the signatures contained within the headers of various file formats.
<?php
function get_img_type($data) {
$magics = array(
'ffd8ff' => 'jpg',
'89504e470d0a1a0a' => 'png',
);
foreach ($magics as $str => $ext) {
if (strtolower(bin2hex(substr($data, 0, strlen($str)/2))) == $str) return $ext;
}
return NULL;
}
?>
If required, additional magic signatures can be added to the array, there is a page on Wikipedia with a good list:
http://en.wikipedia.org/wiki/List_of_file_signatures