How to get Image DPI in PHP

John Smith picture John Smith · Jun 13, 2013 · Viewed 22.2k times · Source

I am searching for the code which could help me to get the Image DPI in PHP.

Could any one look into this ?

Thanks in advance.

Answer

Vinoth Babu picture Vinoth Babu · Jun 13, 2013

You can go for some image libraries for that. Eg: Imagick, GD Library...

(OR)

You can use the following function,

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));
} 

Already solved this question here... :)