PHP and ImageMagick

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
maredzki

PHP and ImageMagick

Post by maredzki »

I have a hosted site which has ImageMagick installed locally in my home directory and not compiled with PHP. How can I use that location in my PHP script (instantiate it from that location and not as a PHP complied module), or just call that location from PHP script?

Thanks for your help!
Marek
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PHP and ImageMagick

Post by fmw42 »

I am not a php expert, but you need to know the full path to IM convert (identify, mogrify etc) and use that in your PHP script to prefix convert in the exec command

try this and change the path to convert appropriate for its location on your system

<?php
exec("/usr/local/bin/convert -version",$out,$returnval);
print_r($out[0]);
?>

You should get back the IM version information.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: PHP and ImageMagick

Post by Bonzo »

I have a lot of php examples on my site see signature.

I do not think Imagemagick is compiled with php it is an external program whereas Imagick is.

Try running this and report what result you get:

Code: Select all

<?php
echo "<pre>";
system("type convert");  
echo "</pre>";
?> 
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PHP and ImageMagick

Post by fmw42 »

or perhaps

Code: Select all

<?php
echo "<pre>";
system("which convert");  
echo "</pre>";
?>
I am not sure of the difference between type and which. Both seem to identify the path to convert in my bash shell.

P.S. I tried both type convert and which convert as above and they both successfully report where IM lives.
Last edited by fmw42 on 2010-08-02T14:49:41-07:00, edited 1 time in total.
maredzki

Re: PHP and ImageMagick

Post by maredzki »

Ok, sweet at least we are getting somewhere...

Using:

Code: Select all

<?php
exec("imagemagick/bin/convert -version",$out,$returnval);
print_r($out[0]);
?>
Got a result of:
Version: ImageMagick 6.4.9-8 2009-03-05 Q16 http://www.imagemagick.org

Using:

Code: Select all

<?php
echo "<pre>";
system("which convert"); 
echo "</pre>";
?>
Got a result of:
/usr/bin/convert

Now how can I call the binaries from PHP to get me going? :)

Edited: changed location of the binary in first code example
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: PHP and ImageMagick

Post by Bonzo »

You should now be able to:

Code: Select all

exec("imagemagick/bin/convert input.jpg output.png");
Or neater when using long comands:

Code: Select all

$cmd = "input.jpg -resize 200x200";
exec("imagemagick/bin/convert $cmd output.png");
maredzki

Re: PHP and ImageMagick

Post by maredzki »

I have a simple script:

<?php
try
{
/*** the image file ***/
$image = 'image.jpg';

/*** a new imagick object ***/
$im = new Imagick();

/*** ping the image ***/
$im->pingImage($image);

echo 'Imagick';
}
catch(Exception $e)
{
echo $e->getMessage();
}

?>[/code]

This code just throws the following error:
Fatal error: Class 'Imagick' not found in graphics/test2.php on line 13

I am assuming that it cannot instantiate a class but how do I point it to where ImageMagick is installed?

Thanks much folks!

Marek
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: PHP and ImageMagick

Post by Bonzo »

This is where confusion comes in.

ImageMagick can be run from php as per my examples above as an external program. You are trying to use Imagick which is an API? and needs to be compiled with php ?

I would stick to Imagemagick with the comand line as there are a lot more options although you need to be extra careful of user input if users are allowed to submit to the code.
maredzki

Re: PHP and ImageMagick

Post by maredzki »

Bonzo, you are the Man! I will definitely stick to ImageMagick and will work on the examples you have. Is there a way to get in touch with you directly if I have any questions?

I am currently trying one example of yours to see if it will work on my hosted shared server.

Cheers and thanks much!
Marek
Post Reply