Having difficulty changing DPI on JPEG with PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • markurxn
    New Member
    • May 2008
    • 1

    #1

    Having difficulty changing DPI on JPEG with PHP

    I need a solution to automatically change the DPI of uploaded photos from 300 to 72. I've coded this bit of script to read the 14th through 18th bit of binary data of a JPEG and update the DPI. The script below seems to be writing to the jpeg, but the afterwards, the jpeg is unreadable and when I rerun the script, it still shows the image DPI hasn't changed. Anyone have experience changing DPI of an image?
    [code=php]
    <?php

    $filename = 'familyshot33.j pg';

    // open file for writing
    $f = fopen($filename , 'r+');

    $string = fread($f,20);
    // show dpi before
    echo hexdec(bin2hex( substr($string, 14, 2))) . ' ' . hexdec(bin2hex( substr($string, 16, 2))) .'<br/>';

    // update dpi
    $image = substr_replace( $string, pack("Cnn", 0x01, 72, 72), 13, 5);

    // show dpi after
    echo hexdec(bin2hex( substr($image, 14, 2))) . ' ' . hexdec(bin2hex( substr($image, 16, 2))) .'<br/>';

    // write and close
    fwrite($f, $image, filesize($filen ame));
    fclose($f);

    ?>
    [/code]
    Last edited by Atli; May 30 '08, 07:27 PM. Reason: Added [code] tags.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Markurxn. Welcome to Bytes!

    You may find this thread to be of some interest.

    Comment

    Working...