Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.