How to change Image to jpg and set dpi to 300 using php?

How to change Image to jpg and set dpi to 300 using php?

July 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to change Image to jpg and set dpi to 300 using php?

Changing Image DPI to 300 using PHP

Understanding the Problem

You’re facing an issue where you can’t successfully change the DPI (dots per inch) of your JPEG image using PHP. You’ve got the right idea—you’re using imagejpeg() to convert a PNG image to a JPEG, and then trying to modify the DPI with substr_replace(). However, it seems that the DPI isn’t being updated correctly.

The Current Code

Let’s break down your current code:

$jpgImage = 'data:image/jpeg;base64,' . base64_encode($contents);

$image = file_get_contents($jpgImage);
$image = substr_replace($image, pack("cnn", 1, 300, 300), 13, 5);

header("Content-type: image/jpeg");
header('Content-Disposition: attachment; filename="' . basename($jpgImage) . '"');
echo $image;

The key lines here are:

  • $jpgImage = 'data:image/jpeg;base64,' . base64_encode($contents);: You’re encoding the output of imagejpeg() to a base64 string.
  • $image = file_get_contents($jpgImage);: You’re retrieving this image as a string from the base64 data.
  • $image = substr_replace($image, pack("cnn", 1, 300, 300), 13, 5);: This is where you’re trying to change the DPI.

The Issue

The problem lies in how you’re handling the image after imagejpeg(). Instead of treating it as a binary file, you’re converting it to base64 text data and then back to a string using file_get_contents(). This conversion process is losing the metadata that PHP needs to modify the DPI.

The Solution

To solve this, we need to keep the image in its binary format and manipulate it directly. Here’s how:

public function pngtojpgAction()
{
    // Create a new true color image from the input PNG data
    $input = imagecreatefrompng($this->getRequest()->getParam('imagedata'));
    $width = imagesx($input);
    $height = imagesy($input);

    // Create a truecolor output image and set it to white
    $output = imagecreatetruecolor($width, $height);
    $white = imagecolorallocate($output, 255, 255, 255);
    imagefilledrectangle($output, 0, 0, $width, $height, $white);

    // Copy the input image to the output image
    imagecopy($output, $input, 0, 0, 0, 0, $width, $height);

    // Set the DPI of the output image
    imagesetresolution($output, 300, 300);

    // Output and send the image to the browser
    header("Content-type: image/jpeg");
    header('Content-Disposition: attachment; filename="' . basename($jpgImage) . '"');
    imagejpeg($output);
}

Here are the key changes:

  • We’re using imagecreatefrompng() instead of file_get_contents(). This maintains the binary format.
  • After creating the output image, we set its resolution with imagesetresolution($output, 300, 300);.
  • Finally, we directly output the image with imagejpeg($output); without converting it to base64 or a string.

Conclusion

Changing DPI in PHP can be tricky because of the binary nature of images. To solve this:

  1. Ensure you’re working with the image data in its binary format.
  2. Use the appropriate GD functions (imagecreatefrompng(), imagesetresolution()).
  3. Directly output the image with imagejpeg().

Remember to install the GD library if you haven’t already for these operations to work. You can do this through your package manager (e.g., sudo apt-get install php-gd on Ubuntu).

For more advanced image manipulation, I recommend checking out Intervention Image, a robust image handling library for PHP that simplifies many of these tasks.

Happy coding! Let me know if you have any other questions about image processing in PHP.

SEO Errors

Our network