note 85712 deleted from function.imagejpeg by cmb
| From: | cmb@php.net | Date: | Sun, 20 Oct 2019 09:00:02 +0000 |
| Subject: | note 85712 deleted from function.imagejpeg by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212723@lists.php.net to get a copy of this message | ||
Note Submitter: lorezyra (at) lorezyra (dot) com
----
I noticed through testing that my previous function would not always return the data requested...
this is due to the fact that some cameras and software dont correctly write the DPI info in the JFIF
header... So, my 2nd solution is to read the exif info (if available) and output the first non-zero
value as the DPI...
<?PHP
function getJPEGresolution($filename){
if(exif_imagetype( $filename)!= IMAGETYPE_JPEG) return(false);
$outRez=array();
// Read the file
$exif = exif_read_data($filename, 'IFD0');
ob_start(); // start a new output buffer
$image = file_get_contents($filename);
// grab DPI information from the JPG header
$outRez["xDPI"] = (int)(ord($image[15])>0? ord($image[15]) :
$exif['XResolution'] );
$outRez["yDPI"] = (int)(ord($image[17])>0? ord($image[17]) :
$exif['YResolution'] );
ob_end_clean(); // stop this output buffer
//correct output if header doesn't contain dpi info:: use exif info instead
$outRez["xDPI"] = ($outRez["xDPI"]>0? $outRez["xDPI"] :
$exif['THUMBNAIL']['XResolution'] );
$outRez["yDPI"] = ($outRez["yDPI"]>0? $outRez["yDPI"] :
$exif['THUMBNAIL']['YResolution'] );
//double check values; make sure it's just a number and not "72/1" ...
if(!is_numeric($outRez["xDPI"])) $outRez["xDPI"] =
(int)substr($outRez["xDPI"], 0, strpos($outRez["xDPI"],"/",1));
if(!is_numeric($outRez["yDPI"])) $outRez["yDPI"] =
(int)substr($outRez["yDPI"], 0, strpos($outRez["yDPI"],"/",1));
//xDPI and yDPI should equal in value... but we output both anyway...
return($outRez);
}//end function getJPEGresolution
?>