Yeni Setiawan


Get DPI value of an image using PHP

19 February 2008 14:08:30 WIB, under category PHP

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.


14 Comments so far..

  1. tom
    08 April 2008, 11:12:05 WIB

    hi,
    this is an excellent little script that i found from the forum you mention! for some reason though i am getting the values 100, 100 returned from both a 72dpi file and a 300 dpi file…?
    Would you know why this is at all? I have resized both images in photoshop and saved out at the DPI’s mentioned.

    Any help would be greatly appreciated!

    Thanks,

    Tom

  2. moe
    10 April 2008, 09:32:59 WIB

    hi,
    i know this is way offfff topic, but you think you know how to get a Snap shot of a website in different sizes? if so, could you post it on as a blog or maybe give some ideas and point us to few tutorials that maybe helpful?

    thanks a bunch!

  3. PHP ile resim dosyas?n?n DPI bilgisini alal?m - Günlük Haftal?k Ayl?k
    15 May 2008, 20:34:17 WIB

    [...] Kaynak: Get DPI value of an image using PHP [...]

  4. john
    23 July 2008, 08:40:59 WIB

    Man, this is a great function that works really well. However, it seems it can’t read DPI information of a JPEG created on a MAC. I guess encoding methods differ. Do you know where the DPI information is located on a MAC-created JPEG?

  5. MS
    19 October 2009, 21:21:50 WIB

    This doesn’t work at all, and I like the author writing:
    print_r(get_dpi2(‘filename.jpg’));

    when he calls the function “get_dpi” and then call “get_dpi2″ for some reason that I can’t seem to understand.

    Lovely function, beside the non-working part.

  6. Sindre
    30 October 2009, 05:56:27 WIB

    For some reason i get a DPI value: 17490
    Isnt this to high? Its just a png-logo..

  7. Rosario Rojas
    27 November 2009, 15:22:24 WIB

    This is a very good code. Saved me a lot of work to modify more than 1,000 JPG images.

    Thank you very much!!!.

  8. Mohammad
    04 March 2010, 23:31:18 WIB

    Nice work!
    This works well with JPEG images but not for PNG images.

  9. Sebastian
    01 June 2011, 22:43:55 WIB

    Sure this would be much easier to do using the imageMagick library. Imagemagick::getImageResolution

    That should save a lot of hacking.

  10. Sartaj
    19 July 2011, 13:55:26 WIB

    The above code is not working for uploaded image please tell me how to get DPI of uploaded image using php.

  11. Damy
    18 October 2011, 17:47:29 WIB

    I was strugeling with this, and since I found it, her 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,0,4);
    return array(hexdec($x),hexdec($y));
    }

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

    Cheers

  12. asd
    23 February 2014, 07:47:59 WIB

    hahahaha

  13. asd
    23 February 2014, 07:49:06 WIB

    testing

  14. Zinfoway Tech
    05 June 2014, 20:09:31 WIB

    Thanks for sharing this useful code..

RSS for comments TrackBack URL

Leave a comment or two

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>