note 97033 deleted from function.imagejpeg by danbrown

From: Date: Mon, 29 Mar 2010 15:21:01 +0000
Subject: note 97033 deleted from function.imagejpeg by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-167495@lists.php.net to get a copy of this message
Note Submitter: php at webdecker dot de ---- 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; } ?>

« previous php.notes (#167495) next »