This is a simple function I wrote to get a DPI (dot per inch) value from an image using PHP only, without the need of ImageMagick nor GD library. The script was inspired from denisb post (the third post). At the forum, he described how to get the DPI value stored within the file.
< ?php function get_dpi($filename){ // open the file and read first 20 bytes. $a = fopen($filename,'r'); $string = fread($a,20); fclose($a); // get the value of byte 14th up to 18th $data = bin2hex(substr($string,14,4)); $x = substr($data,0,4); $y = substr($data,4,4); return array(hexdec($x),hexdec($y)); } // output the result: print_r(get_dpi2('filename.jpg')); ?>
I have tried this function to get DPI value of an image that I generated using Photoshop and worked as expected but failed to get DPI value from an image from any digital camera. It returned weird value instead.
Any idea why this function failed to retrieve DPI value from image that generated by digital camera? Please share your opinion.
Leave a Reply