Jump to content

[SOLVED] Having difficulty changing DPI on JPEG with PHP


markurxn

Recommended Posts

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

 

?>

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)));
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.