markurxn Posted May 28, 2008 Share Posted May 28, 2008 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? <?php $filename = 'familyshot33.jpg'; // 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($filename)); fclose($f); ?> Link to comment https://forums.phpfreaks.com/topic/107671-solved-having-difficulty-changing-dpi-on-jpeg-with-php/ Share on other sites More sharing options...
MadTechie Posted May 28, 2008 Share Posted May 28, 2008 code looks ok try this <?php $filename = 'image.jpg'; $dpi = 96; if(!file_exists($filename)) { die("error, file not found"); } $image = file_get_contents($filename); echo "changed:"; echo hexdec(bin2hex(substr($image, 14, 2))) . ' ' . hexdec(bin2hex(substr($image, 16, 2))); $image = substr_replace($image, pack("Cnn", 0x01, $dpi, $dpi), 13, 5); file_put_contents($filename, $image); echo "<br> to <br>"; echo hexdec(bin2hex(substr($image, 14, 2))) . ' ' . hexdec(bin2hex(substr($image, 16, 2))); ?> Link to comment https://forums.phpfreaks.com/topic/107671-solved-having-difficulty-changing-dpi-on-jpeg-with-php/#findComment-552044 Share on other sites More sharing options...
markurxn Posted May 28, 2008 Author Share Posted May 28, 2008 Wonderful, that did the trick. Thanks! Link to comment https://forums.phpfreaks.com/topic/107671-solved-having-difficulty-changing-dpi-on-jpeg-with-php/#findComment-552197 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.