Change DPI + resample

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
jeffreyg
Posts: 14
Joined: 2014-02-10T03:42:50-07:00
Authentication code: 6789

Change DPI + resample

Post 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?
jeffreyg
Posts: 14
Joined: 2014-02-10T03:42:50-07:00
Authentication code: 6789

Re: Change DPI + resample

Post 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.
Post Reply