note 86858 added to function.imagejpeg
| From: | soapergem at gmail dot com | Date: | Thu, 06 Nov 2008 21:48:21 +0000 |
| Subject: | note 86858 added to function.imagejpeg | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-146255@lists.php.net to get a copy of this message | ||
I came here looking for something similar to the getJPEGresolution function, but noticed the
drawbacks that were pointed out in the last post. So, after drawing on some other code examples on
the web, I put together the following function which should always properly return the correct
values. (But remember that you still need to have the EXIF extension installed with your instance of
PHP for this to work!)
<?php
function jpeg_dpi($filename)
{
if ( exif_imagetype($filename) != IMAGETYPE_JPEG ) {
return false;
} else {
$exif = exif_read_data($filename, 'IFD0');
}
$x = $y = 0;
if ( isset($exif['XResolution']) && isset($exif['YResolution']) ) {
$x = intval(preg_replace('@^(\\d+)/(\\d+)$@e', '$1/$2',
$exif['XResolution']));
$y = intval(preg_replace('@^(\\d+)/(\\d+)$@e', '$1/$2',
$exif['YResolution']));
}
if ( !$x && !$y && $fp = fopen($filename, 'r') ) {
$string = fread($fp, 20);
fclose($fp);
$data = bin2hex(substr($string, 14, 4));
$x = hexdec(substr($data, 0, 4));
$y = hexdec(substr($data, 4, 4));
}
if ( $x || $y ) {
return array($x, $y);
}
return false;
}
?>
This function returns an array with the x-resolution, y-resolution when they can be determined,
otherwise FALSE.
----
Server IP: 209.41.74.194
Probable Submitter: 66.170.4.122
----
Manual Page -- http://www.php.net/manual/en/function.imagejpeg.php
Edit -- https://master.php.net/note/edit/86858
Del: integrated -- https://master.php.net/note/delete/86858/integrated
Del: useless -- https://master.php.net/note/delete/86858/useless
Del: bad code -- https://master.php.net/note/delete/86858/bad+code
Del: spam -- https://master.php.net/note/delete/86858/spam
Del: non-english -- https://master.php.net/note/delete/86858/non-english
Del: in docs -- https://master.php.net/note/delete/86858/in+docs
Del: other reasons-- https://master.php.net/note/delete/86858
Reject -- https://master.php.net/note/reject/86858
Search -- https://master.php.net/manage/user-notes.php