Page 1 of 1

Change DPI + resample

Posted: 2014-02-14T01:44:27-07:00
by jeffreyg
I'm trying to change the DPI and of an image to make the output the same as when I change the DPI in Photoshop. I already got it working with ImageMagick, but now I want it to work with Imagick as well. I have to change the DPI of the input image to 100 (because I need to print it).

My input image has these specifications (Photoshop):
Width: 700px
Height: 500px

Document size:
Width: 18,52 cm
Height: 13,23 cm
Resolutation (DPI): 96


My output image has these specifications (using ImageMagick and Imagick and Photoshop to open them again):

Output ImageMagick:
Width: 729px
Height: 521px

Document size:
Width: 18,52 cm
Height: 13,23 cm
Resolutation (DPI): 100


Output Imagick:
Width: 700px
Height: 500 px

Document size:
Width: 17,78 cm
Height: 12,70 cm
Resolutation (DPI): 100


This is the code I'm using with ImageMagick (PHP):

Code: Select all

exec("convert -strip -units PixelsPerInch  input.jpg -resample 100 -set density 100  output.jpg");
And this is the code I'm using with Imagick:

Code: Select all

$img = new Imagick('input.jpg');
$img->stripImage();
$img->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$img->setImageResolution(100,100);
$img->resampleImage(100,100,imagick::FILTER_UNDEFINED,1);
$img->writeImage('output.jpg');
$img->destroy();
So its changing my cm and not my px. How do I get the exact same output with Imagick?

Re: Change DPI + resample

Posted: 2014-02-14T06:01:26-07:00
by jeffreyg
Found the solution..

New code:

Code: Select all

$img = new Imagick('input.jpg');
$img->stripImage();
$img->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$img->setResolution(100,100);
$img->resampleImage(100,100,imagick::FILTER_UNDEFINED,1);
$img->writeImage('output.jpg');
$img->destroy();
Old code:

Code: Select all

$img = new Imagick('input.jpg');
$img->stripImage();
$img->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$img->setImageResolution(100,100);
$img->resampleImage(100,100,imagick::FILTER_UNDEFINED,1);
$img->writeImage('output.jpg');
$img->destroy();
I had to use: setResolution instead of setImageResolution.