International PHP Conference Munich 2025

Voting

: max(seven, two)?
(Example: nine)

The Note You're Voting On

Michaelsoft
17 years ago
[Editor's note: fixed according to the note of roberto at ilpiola.it]

I could not find any information on changing the DPI information on a JPG file using the GD lib. Since changing this does not resize or scale the actual image, it is only a header-setting.
The following snipplet will save your $image to $file and set the DPI to 150.

<?php

imagejpeg
($image, $file, 75);

// Change DPI
$dpi_x = 150;
$dpi_y = 150;

// Read the file
$size = filesize($file);
$image = file_get_contents($file);

// Update DPI information in the JPG header
$image[13] = chr(1);
$image[14] = chr(floor($dpi_x/256));
$image[15] = chr( $dpi_x%256);
$image[16] = chr(floor($dpi_y/256));
$image[17] = chr( $dpi_y%256);

// Write the new JPG
$f = fopen($file, 'w');
fwrite($f, $msg, $size);
fclose($f);

?>

P.s. not fully tested (yet) but it works for my images ...

<< Back to user notes page

To Top