Do some ImageMagick

July 20, 2009 by nuno costa 2 comments Stumble It del.icio.us

One line image resizing ?

No more Out of memory or Low memory errors !

I’m in heaven!

Check out phMagick – php & imagemagick the easy way

What’s wrong with GD ?


Actually there is nothing wrong with GD, the problem is memory consumption!

It isn’t hard to find cell phones or digital cameras producing images of 1600x1200px and having several Mb in size

I’m no photographer so my 6MP low cost digital camera works like a charm, the problem is that most shared hosting companies aren’t as happy as me with my digital camera mostly when I want to do some image manipulation, why?

Because of memory consumption!

If you do image manipulation with Php GD library you are aware of this, often enough you get “unable to allocate x bytes of memory” errors.

A simple thumbnail resize forces you to open the image in true color mode resize it and save it, an 1600X1200px 300kb jpeg file when opened in an image editor (such as Gimp) will consume about 18mb of memory, the same is true for Php GD

The Magick trick!

I’m all in favour of simple code, and let’s face it the GD way is not simple!

Take a look at the typical code to resize an image taken from Christian Heilmann’s

function createthumb($name,$filename,$new_w,$new_h){
        $system=explode('.',$name);
        if (preg_match('/jpg|jpeg/',$system[1])){
                $src_img=imagecreatefromjpeg($name);
        }
        if (preg_match('/png/',$system[1])){
                $src_img=imagecreatefrompng($name);
      }
 
old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y) {
        $thumb_w=$new_w;
        $thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y) {
        $thumb_w=$old_x*($new_w/$old_y);
        $thumb_h=$new_h;
}
if ($old_x == $old_y) {
        $thumb_w=$new_w;
        $thumb_h=$new_h;
}
 
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
 imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
 
if (preg_match("/png/",$system[1]))
 
{
        imagepng($dst_img,$filename);
} else {
        imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}

And the ImageMagick way

	convert -resize "100x100>"; originalfile thumbnailfile

It’s a lot of code just to resize one image!

One can argue that ImageMagick does the same but you just don’t see the code behind, yes indeed it does, I just want to quickly resize an image not recreate GIMP.

Also It’s kind of lame GD doesn’t recognize the image format automatically.

The wonderful thing about ImageMagick is that allows you to skip Php memory limits because it’s executed in another process and also it is much more easy to use!

There are several Php libraries natively supporting it, but some are still in beta and all require Php to be compiled with support for them, it may be ok in your own server, but on shared hosting it’s almost impossible to convince them to recompile Php just for you!

ImageMagick comes installed by default on almost every linux distribution, so it’s present on almost all shared hosting services, and it is easy to ask them to install ImageMagick than to recompile Php!

As a last resource you can compile ImageMagick with linked libraries and upload it to your server

What can we do with it ?

Actually anything you can remember to do to an image it can be done with ImageMagick, but we will focus on the most common tasks

Interested ? Try phMagick – php & imagemagick the easy way

Let me know what you think, leave a comment

2 comments so far Add Your Comment

  1. by chad on February 6 2010 at 20:51

    do you know the links to phMagick are all down?

  2. by nuno costa on February 7 2010 at 13:08

    @chad

    They are not down, the old pages where replaced (last year) by a new word press blog, the. htacess redirects where becoming to difficult to maintain, so I remove them

    that’s why some external links won’t give you the correct page

More from francodacosta.com

© francodacosta.com - All rights reserved