Several times in my career i have played around with ImageMagick and have always found the documentation of its usage with PHP to be severely lacking. Or in most cases though the problem originates with the server.
Why can’t I get ImageMagick to work?
Either its not installed on your shared host, or its installed and crippled so you can’t use php functions like “exec(), or system()”. Its good to mention that if you are running into this problem you may want to check out some API’s that hook into ImageMagick like Imagick for PHP, or MagickWand. Both are very good API’s but, you will find less examples working with them then the original ImageMagick documentation.
Now, before we get into an example, I would like to point out that most of the examples around the net are using personal installs of ImageMagick, or from a person that has root access to a dedicated server. This allows these code guru’s to edit the Apache server to accept functions that most shared hosting has turned off.
The first time I tried ImageMagick I installed a virtual machine on my computer running Linux(Umbuntu) and I used and SSH connection to run bash commands on the local server that had the perfect install of ImageMagick that let me do whatever I wanted. Here the bad thing about that, It took FOREVER! And not only was I unsure about the security of my server but I also didn’t know how it would effect and actual server CPU, of even if all my code would work on the server that I moved it to.
So, the perfect solution came to me the other day when I ran phpinfo() on my current host HostGator, and found that ImageMagick was installed. Within minutes I was running ImageMagick commands on my development domain, AWESOME!
So, Long story short, don’t try installing ImageMagick unless you have all the time in the world and your an Apache guru, just use HostGator!
Is ImageMagick installed on my current host?
The first thing you want to do before you begin working with ImageMagick is make sure that its installed on your host’s server, along with what font’s you can use and what convert commands are installed on the server.
note: The following code will assume that you have FTP access to your server and have basic knowledge of PHP.
Just create a page called magicktest.php and paste in the following code
[php]
<?php
header(‘Content-Type: text/plain’);
system("exec 2>&1; type convert");
system("exec 2>&1; locate */convert");
system("exec 2>&1; convert -version");
system("exec 2>&1; convert -list type"); // << before IM v6.3.5-7
system("exec 2>&1; convert -list font");
?>
[/php]
This will run a few of commands on your server to see what is present.
- header (tells the browser what type of content will be displayed, nothing before this or you will get a header error)
type(Tells you ifconvertis available on the command PATH and where it is)locate(finds all convert commands that exist on the server, including ones that are NOT ImageMagick commands)convert -version(prints the version number of convert)convert -list type(lists fonts for an older version of ImageMagick)convert -list font(lists the fonts that ImageMagick has access to)
If all you see ar errors, then “convert” is not on the command line path, and your ISP provider did not initialize the web server PATH properly to include it. If this is the case you will need to find out exactly where it is located and use something like this for you PHP scripts, which makes your script less portable. For example suppose the “convert” command is in “/opt/php5extras/ImageMagick/bin“, then you can set that in a variable at the top (for quick changes for different ISP hosts), and directly specify its location. For more explanation on this see the master of all things ImageMagick Anthony Thyssen and this post.
Now once you have all that information save it somewhere, you will always need to reference the convert path or fonts installed.
Can I use exec() to run commands on my host?
An easy way to test and see if exec() is available and working on your server is to run the following code provided by php.net.
[php]
// outputs the username that owns the running php/httpd process
// (on a system with the "whoami" executable in the path)
echo exec("whoami");
?>
[/php]
If you see your user name then its all working fine. If not, you may want to try system(), and/or passthru(). Actually in most cases if your code doesn’t work try system() or passthru(), I will be using a combination of the three in my post examples.
Difference between ImageMagick images and normal image files
The basic idea of ImageMagick is that you want the server to adjust your images just like you could do in photoshop. But, you won’t be referencing your images the same as <img src="myimage.jpg" />. With photoshop you edit and image then save it as a file then reference the file, src=”myfile.jpg”. With ImageMagick, you ask the server to do the image edits and then it returns code that builds the image in the browser, this is the main reason why we need header() at the top of your php file. If you didn’t specify headers before your code you would get a bunch of crazy characters all over the page.
Here is how its done. You create a file called rotateImage.php, then you reference it just like and image, <img src=”rotateImage.php” />. So you pull the php file in through the image tag and it displays your image as if the php file WAS an image file. Very cool right!!
Examples!! Rotate

Original test image
Rotate image 45 degrees
The magick! Rotate and image with just two lines of code!
[php]
<?php
header( ‘Content-Type: image/jpg’ );
system("convert saob.jpg -rotate 45 jpg:-");
?>
[/php]
Example: Annotate, negate, resize…
Several commands in one!
The magick! Annotate, Negate, Resize, swap, composite
[php]
<?php
header( ‘Content-Type: image/jpg’ );
$color="white";
$image="saob.jpg";
$scale="200%";
$size="140×96";
$string="Senseless Acts of Beauty";
passthru("convert -background none -fill ‘$color’ -gravity center" .
" -font AvantGarde-Book -size ‘$size’ caption:’$string’" .
" \\( ‘$image’ -negate -resize ‘$scale’ \\) -swap -2,-1 -composite" .
" jpg:-" );
?>
[/php]
Its almost limitless of what you can do with ImageMagick so, that being said, I plan on making a bunch of short posts just showing examples of ImageMagick usage along with the code on how you can do the same. As always if you have any questions just post a comment. THANKS!!
1 Response to: ImageMagick usage and examples with PHP: #1
I’ve been searching long for collecting the output from imagemagick through passthru, instead of saving it, your code solved it. Thanks.