Retrieve Image Resolution(DPI) of an image (JPEG,PNG,SVG,GIF) without using any PHP extension

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 07:14:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!