3

I recently started using imagemagick with php and I'm relatively new with both of these, IM and PHP. So, I'm here to ask for some help/suggestion(s).

First

If lets say a user uploads a gif or a png image on my site and I want it converted to jpg, is there any command like for example.$image->convert($file,'.jpg) or the convert command is accesible only thru exec() ? like for example exec(convert picture.png to picture.jpg)

Second

What if for again, the user uploads gif/png on the site and I resize the image to a specified width/height and write the image, with the writeImage function of IM like this: $image->writeImage(basename.$ext) where $ext = jpg. Will this work properly,is this a good practice? I assume this will only rename it but still, I don't see a problem in this... o.O

Oh sorry one more question, I'm not very familiar with exec, is it better using imagemagick using exec(), does it improve speed, load etc?

Community
  • 1
  • 1
inrob
  • 4,969
  • 11
  • 38
  • 51
  • `exec()` is an [exploitable PHP function](http://stackoverflow.com/questions/3115559/exploitable-php-functions) hence you'll find it disabled on most servers. – Salman A May 12 '12 at 21:51
  • 2
    I have not seen it disabled on "most servers". As with everything you need to be careful about user input. – Bonzo May 12 '12 at 22:00

2 Answers2

4

I can't answer your questions directly but thought I point you to a few resources:

Regarding which is better, exec or the PHP extension, I asked this same question a few months ago:

Should I use a PHP extension for ImageMagick or just use PHP's Exec() function to run the terminal commands?

For all Image Magick PHP functions you should look up the official guide:

http://www.php.net/manual/en/book.imagick.php

I switched from Image Magick to Graphics Magick as I heard it has better performance. It is a fork of Image Magick with an emphasis on performance. Large sites like Flickr and Etsy use it:

http://www.graphicsmagick.org/

This guide got me started:

http://devzone.zend.com/1559/manipulating-images-with-php-and-graphicsmagick/

And they have their own manual on php.net:

http://php.net/manual/en/book.gmagick.php (most of the commands are identical to Image Magick's)

Community
  • 1
  • 1
1

I prefer using exec() as it is supported a lot better than Imagick ( the example code you posted ), easier to use and supports all the operators ( depending on your version ) which again Imagick does not. Some of the Imagick code works differntly in the different versions; there are a lot more Imagick examples around than there were a couple of years ago when I first started looking at it.

Saying that Imagick can be faster and is/can be? built into php although some people have trouble installing it.

I belive that this site http://valokuva.org/?cat=1 belongs to a Imagick developer and has lots of examples.

My site www.rubblewebs.co.uk/imagemagick has lots of examples of php with exec( )

As to your other two questions yes you can change the file type with Imagick and I am not sure about basename.$ext - why not try it? As long as basename does not have an extension it may work but you might need to include basename.$ext in quotes.

As to speed it dpends sometimes GD is faster other times Imagick or Imagemagick. You can always do some tests. I did some a year or so ago and you can try the code on your server. http://www.rubblewebs.co.uk/imagemagick/speed/Speed_tests_1.pdf

Bonzo
  • 5,169
  • 1
  • 19
  • 27
  • Hello Bonzo. Thankyou for your reply. I appreciate it. Thanks for the links provided. I think I've visited your website sometime ago when I was doing some research about IM. Anyways regarding the filetype change, what is the alternative you propose? setimagetype ? or some equivalent one ? – inrob May 12 '12 at 22:14