mononym
mononym

Reputation: 2636

how can i change the dpi of an image with the imagick extension

I need to change all uploaded files to 72 dpi. I'm using the php imagick extension.

heres what i've tried (the image i'm using is 300dpi):

$image = new Imagick();
$image->setResolution(72,72) ;
$image->readImage($img);
$image->resampleImage  (72,72,imagick::FILTER_UNDEFINED,1);
$image->writeImage($target)

this doesn't seem to anything. the image is uploading, but stays at 300dpi

Upvotes: 10

Views: 16628

Answers (3)

yohanes
yohanes

Reputation: 31

use this its work with imagick extension :

$finalImageOnline = $canvas->getCore(); //get Imagick object
    $finalImageOnline->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
    $finalImageOnline->setImageResolution(72,72);
    $finalImageOnline->resampleImage  (72,72,imagick::FILTER_SINC,1);
    $finalImageOnline->setImageFormat("jpg");
    file_put_contents ($this->path . '/' . 'online' . '/' .$namaFile.'.jpg', $finalImageOnline);

Upvotes: 0

Isius
Isius

Reputation: 6974

MatTheCat's answer is spot on. You might also try setImageUnits() to ensure it's working with inches and not centimeters.

$image->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$image->setImageResolution(72,72);

Resampling isn't necessary just to change dpi.

Note that changing the dpi alone will not affect file size and only applies to resampling and printing.

Upvotes: 12

MatTheCat
MatTheCat

Reputation: 18721

It seems you have to use setImageResolution rather than setResolution : http://www.php.net/manual/fr/function.imagick-setresolution.php#95533

Upvotes: 2

Related Questions