note 94178 added to function.getimagesize
| From: | geoff at spacevs dot com | Date: | Wed, 21 Oct 2009 06:22:14 +0000 |
| Subject: | note 94178 added to function.getimagesize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-161932@lists.php.net to get a copy of this message | ||
This function returns the width and height of a JPEG image from a string, allowing the dimensions of
images stored in a database to be retrieved without writing them to the disk first, or using
"imagecreatefromstring" which is very slow in comparison.
<?PHP
function getJPEGImageXY($data) {
$soi = unpack('nmagic/nmarker', $data);
if ($soi['magic'] != 0xFFD8) return false;
$marker = $soi['marker'];
$data = substr($data, 4);
$done = false;
while(1) {
if (strlen($data) === 0) return false;
switch($marker) {
case 0xFFC0:
$info = unpack('nlength/Cprecision/nY/nX', $data);
return array($info['X'], $info['Y']);
break;
default:
$info = unpack('nlength', $data);
$data = substr($data, $info['length']);
$info = unpack('nmarker', $data);
$marker = $info['marker'];
$data = substr($data, 2);
break;
}
}
}
?>
Doing this 10,000 times takes 0.43 seconds, compared with using
imagecreatefromstring/imagesx/imagesy which takes around 1.52 seconds to do the same.
Do not use this instead of getimagesize when dealing with files, getimagesize is much faster coming
in at 0.15 seconds.
----
Server IP: 117.55.229.1
Probable Submitter: 220.233.193.10
----
Manual Page -- http://www.php.net/manual/en/function.getimagesize.php
Edit -- https://master.php.net/note/edit/94178
Del: integrated -- https://master.php.net/note/delete/94178/integrated
Del: useless -- https://master.php.net/note/delete/94178/useless
Del: bad code -- https://master.php.net/note/delete/94178/bad+code
Del: spam -- https://master.php.net/note/delete/94178/spam
Del: non-english -- https://master.php.net/note/delete/94178/non-english
Del: in docs -- https://master.php.net/note/delete/94178/in+docs
Del: other reasons-- https://master.php.net/note/delete/94178
Reject -- https://master.php.net/note/reject/94178
Search -- https://master.php.net/manage/user-notes.php