Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using ImageMagick with PHP

Third Party Scripts/Modules

Using ImageMagick with PHP

by  Glowball  Posted    (Edited  )
Quoting ImageMagick.org, "ImageMagick... is a free software suite to create, edit, and compose bitmap images. It can read, convert and write images in a large variety of formats. Images can be cropped, colors can be changed, various effects can be applied, images can be rotated and combined, and text, lines, polygons, ellipses and BTzier curves can be added to images and stretched and rotated."

It is like the PHP GD Library but much more robust. It also produces nice thumbnails from JPEG images when the GD Library falls short.

Visit http://www.imagemagick.org for a good overview of ImageMagick.

Go to http://www.cit.gu.edu.au/~anthony/graphics/imagick6/ to get more details about what it does and what you can do with it.

This FAQ will get you started working with ImageMagick for PHP. First you need to verify with your host that ImageMagick is installed. Then you need to find out if your PHP installation is as an Apache module or as CGI (phpsuexec).

Step One: Make a .php file for the ImageMagick code.

Step Two: Make a subdirectory where that .php file is that will hold your test image. It needs to be writable (777 permissions) so you can write the new image to it.

Step Three: Put one of the following blocks of code on your PHP page, based on your installation type.

If your server has PHP installed as an Apache module then call ImageMagick like this:

Code:
$test_directory = 'test'; 
$command = "convert -background lightblue -fill blue -font 'Arial' -pointsize 72 label:'Testing' $test_directory/mytest.gif"; 
$test_image = system($command, $returnVal); 
if ($returnVal > 1) echo "ERROR"; 
else echo "<img src=\"$test_directory/mytest.gif\">";

If your server has PHP installed as CGI (phpsuexec) then you can't use system(). You also can't run your script from a directory with 777 permissions so don't try that. Try this code:

Code:
$test_directory = 'test'; 
$path_to_imagemagick = '/usr/bin/convert'; 
$command = "convert -background lightblue -fill blue -font 'Arial' -pointsize 72 label:'Testing' $test_directory/mytest.gif"; 
passthru("\"$path_to_imagemagick\" $command", $returnVal); 
if ($returnVal > 1) echo "ERROR"; 
else echo "<img src=\"$test_directory/mytest.gif\">";

Note: if you are creating thumbnails and the -thumbnail command isn't working, try -resize instead (with the same parameters). Have fun!
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top