[« ImageMagick: a few incantations that I find useful] [Main Index] [lighted case badge mod: linear style »]

More ImageMagick: using ImageMagick from PHP scripts


PHP offers a substantial set of image manipulation libraries, but sometimes calling external programs (ImageMagick in this case) gets the job done better, or lets you do things you couldn't do. PHP programmers aren't as comfortable invoking external programs as CGI programmers, so here's a quick illustration of how to manipulate images with ImageMagick from your PHP scripts.

It's simple enough to use system() in a PHP script to exec an external program. But ImageMagick sometimes has some output you want to deal with. Here's the way I tend to do it:

system("mogrify -geometry '400x300' -quality 40 $uploadname 2>&1 &");

The argument to system() is the command you're executing. And it ought to look familiar if you read my ImageMagick Incantations article. In this case $uploadname is just the name of a file, and the bits at the end handle output redirection and invoke the process in the background. 2>&1 makes the STDERR redirect to STDOUT so you have a chance at debugging an error. & makes the job execute in the background. Since ImageMagick is rewriting a file, we can move on without waiting for it to finish.

Pretty much any ImageMagick command can be used this way.

text, scripts and images copyright © 2001-2011 . All rights reserved.