rooz
rooz

Reputation: 371

PHP: changing DPI of an image using imagick?

I'm trying to figure out how to increase an image DPI using PHP and imagic.

However, everytime I use the following code, my page returns a 500 error!

this is the code:

$im = new Imagick();
$im->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$im->setImageResolution(300,300);
$im->readImage("test.png");
$im->setImageFormat("png");
header("Content-Type: image/png");
echo $im;

I know image magic is installed as I am using a VPS and I also tried this code which works fine:

<?php
exec("/usr/bin/convert bb9yuui70.png -bordercolor black -border 10x10 bb9yuui70.png");
?>
<img src="bb9yuui70.png">

Is there something that i am missing? I am quite new to the world of imagic.

any help would be appreciated.

Upvotes: 3

Views: 1166

Answers (1)

fusion3k
fusion3k

Reputation: 11689

500 server error → Take a look at webserver errors log1 and you will see:

Fatal error: Class 'Imagick' not found

or

Fatal error: Uncaught exception 'ImagickException' with message 'Can not process empty Imagick object'...

If you see the first error, you have to install Imagick php module (it is not enough to have imagemagick installed).

But, also with Imagick installed, your code fails, because you have first to load the image, then to set units, resolution, etc...

$im = new Imagick();
$im->readImage("test.png");                                   # <--------
$im->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$im->setImageResolution(300,300);
$im->setImageFormat("png");
header("Content-Type: image/png");
echo $im;

1 Actually, two above errors can also be displayed without looking at webserver logfile: next time, place ini_set( 'display_errors', 1 ); error_reporting( E_ALL ); at top of your script and you will see the most errors directly on your page (then, remove it on production).

Upvotes: 4

Related Questions