Page 1 of 1

PHP and ImageMagick

Posted: 2010-08-02T13:40:45-07:00
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

Re: PHP and ImageMagick

Posted: 2010-08-02T13:53:31-07:00
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.

Re: PHP and ImageMagick

Posted: 2010-08-02T13:55:52-07:00
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>";
?> 

Re: PHP and ImageMagick

Posted: 2010-08-02T14:03:00-07:00
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.

Re: PHP and ImageMagick

Posted: 2010-08-02T14:20:46-07:00
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

Re: PHP and ImageMagick

Posted: 2010-08-02T14:25:30-07:00
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");

Re: PHP and ImageMagick

Posted: 2010-08-02T14:28:44-07:00
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

Re: PHP and ImageMagick

Posted: 2010-08-02T14:31:58-07:00
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.

Re: PHP and ImageMagick

Posted: 2010-08-02T14:38:29-07:00
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