6

Possible Duplicate:
Get/set DPI with PHP GD/Imagick?

Is there a possible way to check photo dpi with php. I have 300 dpi and 72 dpi photos. but wants a way to calculate dpi automatically.

Community
  • 1
  • 1
Flash
  • 117
  • 1
  • 2
  • 9
  • 1
    Found it for JPEG: http://stackoverflow.com/questions/262090/find-jpeg-resolution-with-php/262117#262117 – Slava May 05 '11 at 07:15

2 Answers2

17

If you want it without Imagick or GD Library. I was struggling with this, and since I found it, here you go.

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,4,4);

    return array(hexdec($x),hexdec($y));
}

and then print the array or do with it what you want.

Alain Tiemblo
  • 36,099
  • 17
  • 121
  • 153
Naeem
  • 179
  • 1
  • 3
2

It's too late for me to check now but I think you're looking for Imagick::getImageResolution() and Imagick::setImageResolution() if you need to change the DPIs.

I don't think this is possible with GD, I believe it "converts" all images to 72 DPIs.

Alix Axel
  • 151,645
  • 95
  • 393
  • 500