Search code examples
phpimagemagickresolutiondpi

ImageMagick doesn't change resolution on picture


I have a project where I have a hotfolder system in place where I drop a file in a folder and it executes an PHP script that does some magic on the file with ImageMagick.

One of the operations I want to do with ImageMagick is the simple process to change the resolution of a picture.

What I want to do is to get the picture in 72 DPI resolution. This is part of my code:

$im = new Imagick();
$im->setResolution(72,72);
$im->readimage($xmlConfig->general->input); //Input is TIF 300 DPI
$im->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$im->setImageResolution(72,72);
$im->resampleImage(72,72,imagick::FILTER_UNDEFINED,0);
$im->setimageformat($xmlConfig->extension); //added in EDIT
$im->writeimage($xmlConfig->general->output); //Output is also 300 DPI Filename is something.jpg added in EDIT
$im->destroy();

So, what am I doing wrong? Because I cannot get this to work. I have tried all combinations available to try to get this correct and also googling this as a maniac and still do not get the result I want.

**Edit:* I noticed when I did some testing that this actually works. But its when I add the part with setimageformat to JPEG and set the filename to something.jpg that it goes wrong.

The JPEG is always 300 DPI even if I have specified 72 above.


Solution

  • I have found that i need to do this to remove any profile attached to the file to be able to do changes to the picture.

    $im->stripimage();