问题
I want to Retrieve Image Resolution(DPI) of an image (JPEG,PNG,SVG,GIF) without using any PHP extension (like imageMagick). I searched everywhere, but I couldn't find a perfect solution. I tried below code (got from link)
function get_dpi($filename){
$a = fopen($filename,'r');
$string = fread($a,20);
fclose($a);
$data = bin2hex(substr($string,14,4));
$x = substr($data,0,4);
$y = substr($data,0,4);
return array(hexdec($x),hexdec($y));
}
But I am not getting the correct Horizontal and vertical DPI. For example, I used an image with 96dpi and 96dpi, but I got (100,100).And this function is only for JPEG file formats.
回答1:
The DPI of an image is usually a matter of fiction. Rarely is an image created where the physical dimensions of the final rendering actually matter (as far as the image itself is concerned). That said, the DPI information is stored in the EXIF data of a JPEG so you can read it from there with the built-in PHP function:
<?php
$filename = "/Users/quentin/Dropbox/Camera Uploads/2016-03-30 21.01.09.jpg";
$exif = exif_read_data($filename);
?>
DPI is <?php echo $exif["XResolution"] ?> by <?php echo $exif["YResolution"] ?>
来源:https://stackoverflow.com/questions/36332823/retrieve-image-resolutiondpi-of-an-image-jpeg-png-svg-gif-without-using-any