22 June 2009 ~ 2 Comments

Image Manipulation in PHP

Sometimes there is a need to do simple image manipulation for certain web projects. Take for example a popular site called Flickr. Flickr is an image hosting site that allows online community to store pictures for others to view. And for some cases some uploaders may have big image files and hence a site like Flickr definitely needs some image processing to reduce the actual image into a smaller dimension, hence smaller file size, to allow quick view before actually downloading the actual size.

I’ve been working with Zend Framework quite sometime for web projects. I found it’s quite interesting to find that Zend Framework does not have any class to perform image manipulation even though a proposal for Zend_Image had been submitted to Zend community as a wrapper of existing PHP interfaces: GD and ImageMagick but unfortunately the proposal was not considered.

And for your information, GD which stands for ‘Gif Draw’ is a standard PHP installation, unlike ImageMagick. If you wish to utilize these libraries, you need to have php extensions called php_gd2 and php_imagick respectively installed and enabled on the web server.

The noticeable difference between GD and ImageMagick is that GD simply does not support image format like TIFF (Tagged Image File Format). If TIFF is not your issue then go for GD. And if you wish to make your development easier, you may be interested to use Thumbnail class by our fellow developer. But if you’re looking into ImageMagick I’m going to show you the steps (from another site) on how to install ImageMagick on your Windows web server as Windows installation is a bit tricky since “pecl install imagick” does NOT work properly.

  1. Download and install ImageMagick software from http://www.imagemagick.org/script/binary-releases.php#windows.
  2. Download pecl-5.2-dev.zip (choose the version relevant to your PHP) from http://snaps.php.net/win32/
  3. Copy php_imagick.dll from the archive you’ve downloaded to your PHP extension folder.
  4. Add the following line to php.ini (in the extensions section):
    extension=php_imagick.dll

    or on WampServer click WampServer icon on taskbar, go to PHP > php.ini and add the above entry.

  5. Restart your server.
  6. Try this example script below to verify your installation.
    header('Content-type: image/tif');
    
    $image = new Imagick('example.tif');
    // Create thumbnail for the specified image file.
    $image->thumbnailImage(100, 0);
    
    echo $image;

    The script should display the image thumbnail with a maximum width of 100 pixels.

If you find the above steps are difficult to follow you may want to download php_imagick.dll here and start from step 3. And if you need help with these libraries, check the manual pages for GD and ImageMagick.

If you find this post helpful, appreciate if you could leave your rating below to indicate that this post is really useful and at the same time let others know about this post. Thank you friend :)

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
Loading ... Loading ...

Don't leave just yet! You may also be interested to take a quick look at my other posts.

  1. Start SOAP with Zend Framework
  2. Read Remote Content or File using Zend
  3. Download Torrent from Web
  4. Simple Steps to Start WCF with Visual Studio 2008
  5. Zend Caveats

2 Responses to “Image Manipulation in PHP”

  1. Kowser 25 December 2009 at 7:33 pm Permalink

    It took 5 days to find a solution…
    and in the end i got work with it.
    It is working with Apache 2.2 and PHP 5.2

    I was not able to make it workingin XAMPP and Zend Server

    I will let u know if there is any good news.

    Thanks a lot


Leave a Reply

Spam protection by WP Captcha-Free