Monday, September 15, 2008

Asido: A PHP Thumbnailer/ Resizer Class

I was using class.upload for some time until a resize function (framing) I was using didn't work properly on my new server setup. So I looked for other PHP image processing classes I can find, but I realized that there is nothing that is actively developed. I also tried phpThumb but I had problems when I used it on some shared hosts.

Asido

I like Asido because of it's simple API. Unfortunately, it also has some problems and the latest release on its website was April 2007 (outdated). The most updated version which is pure PHP5 can be downloaded on the project's SVN repository (click the Download GNU tarball link).

Lets's fix it!

Jpeg Quality
The API on the website didn't show how to adjust the quality when saving files as JPEG. It defaults to 80% quality which is quite low for my taste. So here's how to adjust the quality:

define('ASIDO_GD_JPEG_QUALITY', 90)
require_once('asido/class.asido.php');


Resizing Quality
Another problem is the bad image quality when resizing using GD. There's a simple fix to this. Open class.driver.gd.php and replace imageCopyResized with imageCopyResampled inside the __resize function:

Protected Function __resize(asido_tmp $tmp, $width, $height) {

// create new target
//
$_ = imageCreateTrueColor($width, $height);
imageSaveAlpha($_, true);
imageAlphaBlending($_, false);

$r = imageCopyResampled(
$_, $tmp->target,
0,0,
0,0,
$width, $height,
$tmp->image_width, $tmp->image_height
);

// set new target
//
$this->__destroy_target($tmp);
$tmp->target = $_;

return $r;
}

That's it! now Asido works great! one function that I like about it is it's Frame function (which other classes don't have). What it does is create fixed size thumbnails (aspect ratio is maintained, and the empty spaces are filled with a background color).

8 comments:

  1. thank for that post. you have saved me from a lot of debugging. i like this future too.

    ReplyDelete
  2. Amazing... I was just about to drop it and look for something else

    ReplyDelete
  3. Thanks a lot for this blog. Save a lot of my time dealing with Asido.

    ReplyDelete
  4. Fantastic... Now the clients happy (happier) :)

    ReplyDelete
  5. Fantastic.. the quality tip is exactly what I was looking for.

    ReplyDelete
  6. Thanks a lot,Fantastic.
    It Fix my problems

    ReplyDelete
  7. Thanks !!!
    Спасибо !!!

    ReplyDelete
  8. Thank you! Dziekuje ;)

    ReplyDelete