Installing Imagick on Ubuntu 11.10 and Using it to Dynamically Edit Images
Imagick is a PHP wrapper for the Image Magick library. It lets you use PHP to edit images on the fly. I'm working on a Pictures section of this website, and I found it useful to resize a images as users request them, without having to use GIMP to create a bunch of different sized images and then uploading them all. This install method uses PEAR and assumes you already have Apache and PHP up and running.
First, you need to install a few packages from the repositories:
sudo apt-get install php5-dev php-pear imagemagick libmagick9-dev
Now use PEAR to install Imagick
:
sudo pecl install Imagick
It should tell you that the installation succeeded, and ask you to add a line to php.ini. The line does not need to go in php.ini though, it needs to go in the imagick.ini file instead. This shouldn't exist yet, so create the file:
/etc/php5/apache2/conf.d/imagick.ini
And just add the following line to it:
extension=imagick.so
Now you just need to restart Apache and everything should be ready to go for Imagick:
sudo /etc/init.d/apache2 restart sudo /etc/init.d/apache2 reload
Now you should probably test it by getting a basic example running. In a directory hosted by Apache, create the following php file:
<?php header('Content-type: image/jpeg'); // Config $file = "myPic.jpg"; $x = 600; $y = 450; $image = new Imagick($file); $image->adaptiveResizeImage($x,$y); echo $image; ?>
This file will take the image $file and resize it to the size specified by $x and $y. Make sure you change $file to a real image.
The image "http://localhost/magick.php" cannot be displayed because it contains errors.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>The Personal Blog of Justin McCandless</title> </head> <body> <h1>Justin's Cool Picture</h1> <p>Hey faithful readers, here is a picture from my latest adventure, automatically resized to fit this page even though it is a 10 megepixel beast on the server:</p> <img src = "magick.php"> </body> </html>
<?php header('Content-type: image/jpeg'); $file = $_REQUEST["file"]; $x = $_REQUEST["x"]; $y = $_REQUEST["y"]; $image = new Imagick($file); $image->adaptiveResizeImage($x,$y); echo $image; ?>
<img src = "magick.php?file=myPic1.jpg&x=600&y=450"> <br><br> <img src = "magick.php?file=myPic2.jpg&x=300&y=225">
Lastly, if you're looking for more information and examples about Imagick, php.net is what I used to write this article and it's amazing as always.