How to use php and ImageMagick

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
dt59
Posts: 63
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

How to use php and ImageMagick

Post by dt59 »

Code: Select all

<?php
$input = realpath("image.jpg");
$g = exec("convert $input image.png"); 
if ($g){
	echo "yes";
}else{
	echo"is not working";
}
?>
i wrote the above simple code but is returning false, could anyone suggest what could be wrong or the potential cause?
Last edited by dt59 on 2017-08-02T07:29:34-07:00, edited 1 time in total.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Question

Post by Bonzo »

This is not a php forum ( Imagick is an external program and so might not return anything to php ) and "Question" is a useless title as you have at least two threads with the same title.

Try basing your code on this example:

Code: Select all

exec("convert input.jpg output.png", $output, $return); 
if ($return == "0") { echo '<br>Image generation sucssesful<br>'; } 
else { echo '<br>Image generation failed<br>'; } 
dt59
Posts: 63
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: Question

Post by dt59 »

Bonzo wrote: 2017-08-02T07:22:02-07:00 This is not a php forum ( Imagick is an external program and so might not return anything to php ) and "Question" is a useless title as you have at least two threads with the same title.

Try basing your code on this example:

Code: Select all

exec("convert input.jpg output.png", $output, $return); 
if ($return == "0") { echo '<br>Image generation sucssesful<br>'; } 
else { echo '<br>Image generation failed<br>'; } 
noted, i guess you're OK now, so could please help me.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to use php and ImageMagick

Post by fmw42 »

try

exec("convert logo: logo.gif", $output, $return);

Does that work?

if not, try adding the full path to IM convert in your command
dt59
Posts: 63
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: How to use php and ImageMagick

Post by dt59 »

:D :D :D :D :D :D wow my image magick is now working like a charm . i changed the convert to magick as suggested by fmw42. I thank every one that has reply my "useless Question" like Bonzo would call. thanks Bonzo, snibgo and fmw42. great community indeed.
here is the working one:

Code: Select all

exec("magick input.jpg output.png", $output, $return); 
if ($return == "0") { echo '<br>Image generation sucssesful<br>'; } 
else { echo '<br>Image generation failed<br>'; }
Last edited by dt59 on 2017-08-02T10:44:14-07:00, edited 1 time in total.
dt59
Posts: 63
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: How to use php and ImageMagick

Post by dt59 »

does it mean i have to change all the convert in code to magick? but why
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to use php and ImageMagick

Post by Bonzo »

Windows has a convert program and that may be part of the reason - to stop confusion.

If you installed legacy programs you can still use convert but obviously it is not working for you. So yes you will need to change all converts to magick on any code using V7.

You could add a $convert = 'magick'; at the start of your programs and use $convert in your code. If you go back to a V6 install you only need to change one variable. Otherwise you could have the convert variable in a separate file and include that file in your programs so only changing it once for your whole site. Alternatly you could check for the imagemagick version at the start of your program and set the variable value from the result.
dt59
Posts: 63
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: How to use php and ImageMagick

Post by dt59 »

kk, thanks man. so what if i want to run image magick on the world wide web(live sever)? hw would i install image magick on my c-panel
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to use php and ImageMagick

Post by Bonzo »

You might not be able to and will need to get your host to do it. Otherwise pick a host that has it installed already - make sure it is a later version.

You can use Imagick which is an Imagemagick/php API ( class ) but that is another can of worms.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to use php and ImageMagick

Post by fmw42 »

dt59 wrote: 2017-08-02T10:42:45-07:00 does it mean i have to change all the convert in code to magick? but why
I do not use Windows, but you should be able to set up a symbolic link to map convert to magick. One of the Windows users would have to help.
Post Reply