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: 49
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

How to use php and ImageMagick

Post by dt59 » 2017-08-02T05:32:53-07:00

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: 2844
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Question

Post by Bonzo » 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>'; } 

dt59
Posts: 49
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: Question

Post by dt59 » 2017-08-02T07:27:47-07:00

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: 22081
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: How to use php and ImageMagick

Post by fmw42 » 2017-08-02T09:39:39-07:00

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: 49
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: How to use php and ImageMagick

Post by dt59 » 2017-08-02T10:40:52-07:00

: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: 49
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: How to use php and ImageMagick

Post by dt59 » 2017-08-02T10:42:45-07:00

does it mean i have to change all the convert in code to magick? but why

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

Re: How to use php and ImageMagick

Post by Bonzo » 2017-08-02T11:43:39-07:00

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: 49
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: How to use php and ImageMagick

Post by dt59 » 2017-08-02T11:48:22-07:00

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: 2844
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to use php and ImageMagick

Post by Bonzo » 2017-08-02T12:17:40-07:00

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: 22081
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: How to use php and ImageMagick

Post by fmw42 » 2017-08-02T14:04:32-07:00

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