note 97033 added to function.imagejpeg
| From: | php at webdecker dot de | Date: | Mon, 29 Mar 2010 15:19:58 +0000 |
| Subject: | note 97033 added to function.imagejpeg | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-167494@lists.php.net to get a copy of this message | ||
The below mentioned function getJPEGresolution() did not work for some images I head to deal with.
The calculation of dpi stored in the exif header did not work (exif: 288/3 = 96 dpi) and the unit in
the JFIF-Header was not evaluated.
Here a slightly adjusted function:
<?php
/** Reads JPG-file and returns array with 6 elements: type name, image resource, width, height,
dpi for x, dpi for y.
* Dpi values might be 0 if file format does not provide dpi or other resolution information.
* Usage:
* list($type, $image, $w, $h, $dpix, $dpiy) = readJpeg($filename);
*/
public function readJpeg($filename) {
$type = 'jpeg';
$r = array($type, null, 0, 0, 0, 0);
$image = @imagecreatefromjpeg($filename);
if ($image) {
$w = imagesx($image);
$h = imagesy($image);
$dpix = 0;
$dpiy = 0;
// from: http://php.net/manual/de/function.imagejpeg.php
// soapergem at gmail dot com
// 06-Nov-2008 09:48
$exif = exif_read_data($filename, 'IFD0');
$x = $y = 0;
if (isset($exif['XResolution']) &&
isset($exif['YResolution'])) {
$m = array();
if (ereg('^([0-9]+)/([0-9]+)$', $exif['XResolution'], $m)) {
if ($m[2] != 0) {
$x = $m[1] / $m[2];
} else $x = 0;
} else {
$x = floatval($exif['XResolution']);
}
if (ereg('^([0-9]+)/([0-9]+)$', $exif['YResolution'], $m)) {
if ($m[2] != 0) {
$y = $m[1] / $m[2];
} else $y = 0;
} else {
$y = floatval($exif['YResolution']);
}
}
if (!$x && !$y && $fp = fopen($filename, 'r')) {
// from http://www.fileformat.info/format/jpeg/egff.htm
// last visited 29.3.2010
$string = fread($fp, 20);
fclose($fp);
$units = hexdec(bin2hex(substr($string, 13, 1)));
$jx = hexdec(bin2hex(substr($string, 14, 2)));
$jy = hexdec(bin2hex(substr($string, 16, 2)));
if ($units == 1) {
// resolution in dots per inch
$x = $jx;
$y = $jy;
} else if ($units == 2) {
// resolution in dots per cm
$x = $jx * 2.54;
$y = $jy * 2.54;
}
}
if ($x && $y) {
$dpix = $x;
$dpiy = $y;
}
$r = array($type, $image, $w, $h, $dpix, $dpiy);
}
return $r;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 88.73.62.149
----
Manual Page -- http://www.php.net/manual/en/function.imagejpeg.php
Edit -- https://master.php.net/note/edit/97033
Del: integrated -- https://master.php.net/note/delete/97033/integrated
Del: useless -- https://master.php.net/note/delete/97033/useless
Del: bad code -- https://master.php.net/note/delete/97033/bad+code
Del: spam -- https://master.php.net/note/delete/97033/spam
Del: non-english -- https://master.php.net/note/delete/97033/non-english
Del: in docs -- https://master.php.net/note/delete/97033/in+docs
Del: other reasons-- https://master.php.net/note/delete/97033
Reject -- https://master.php.net/note/reject/97033
Search -- https://master.php.net/manage/user-notes.php