Working in a port of Image Component

Hi guys, I need image functionality (thumbs and so) and I’m currently porting one Drupal interface for working with GD2, making a component that interfaces to it with the aim to build an implementation for ImageMagick too in a while

My first design is to make a component with generic methods, an Interface for the implementations and one implementation for GD2, do you have comments? Am I in right direction?

As soon as I finish I’ll publish something here



Ok, I’m finished :P

Here is the result (no testing yet, but as the code comes from Drupal I suppose it won’t have too many mistakes), just to be placed in compoments directory and ready to use…

Please I want critics… above all about architecture, I’m beginning with all of this of Components and Behaviours and although I’m in my first steps, I’d like to contribute



ok, I’m sorry because yesterday it was late and what I uploaded was a bunch of untested code with many mistakes… today I’ll upload the working version I use to build thumbnails using the strategy of “scale_and_crop” with the gd2 implementation… soon I’ll try to upload the ImageMagic version



Interesting thing. Are you going to create a google code project for it? Is ImageCache port planned?

Hey samdark…

the thing is my first aim was to focus on ImageCache strategy but I quitted because of performance… as you know ImageCache follows the strategy of letting php serving the image (cached and modified) based on some configuration, and from my tests the Apache serving the modified file directly is 4 or 5 times faster than php dumping the same file cached, so first of all I’m porting the core lib in front of GD and ImageMagic and later I’ll try to mount something like ImageCache

it’s soon to mount a project, I’ll try to send code for people to test, and later if the thing sounds interesting I’ll make the project



I think you’ve got it wrong that imagecache is serving images via php. Actually php is involved only in image resizing/filtering process that is triggered first time not yet existing image is requested. Resulting image is saved and then is serving directly without php. That’s if you’ve configured Drupal to use “public” files mode instead of serving them with php.

Anyway, I’m interested in this project.


If you dig in the code what ImageCache does is the next thing: imagine this url that seems to return an image:


…files/imagecache… is a path attached to ImageCache that uses this code:

function imagecache_transfer($path) {

  $size = getimagesize($path);

  $headers = array('Content-Type: '. mime_header_encode($size['mime']));

  if ($fileinfo = stat($path)) {

    $headers[] = 'Content-Length: '. $fileinfo[7];

    _imagecache_cache_set_cache_headers($fileinfo, $headers);


  file_transfer($path, $headers);



to print in the buffer marking it as image/jpeg or whatever it is…

that’s why I changed from Drupal to Yii, Drupal does a lot of things I want and I like but as a counterside, does even more I dont want at all, and for very custom webs, it’s really slow, above all in my case I was using ImageCache intensively, drawing in one page more or less 40/50 images through its filter, and the result was reeeeaaaaally sloooooow

for this reason I’m planning (I have a prototype) a library that GENERATE a cache of files, but files are served directly by Apache (I dont want php serving’em)



Ok, I learned a lot in "last day" and formatted the thing as a proper yii extension so… please test it!!!

With the advise of samdark I changed the focus more towards something like Drupal ImageChache


  • Unpack the zip in extensions dir

  • Add this to your config file:









            'scaleAndCrop'=>array('width'=>100, 'height'=>75),

            'rotate'=>array('degrees'=>30, 'background'=>0xff0000),







Basic explanation: you can create as many presets as you want, in this case I have created one named "thumbs"

that caches files in the dir "webroot / imagecache", and for this preset I added two actions: scaleAndCrop and

later rotate. Currently (look at the code) I support: resize, scale, scaleAndCrop, rotate and crop

  • For testing: create a controller and add for example this action:

  public function actionFile() {

    $thumb = Yii::app()->image->createUrl(

                'thumbs', // the preset we have configurated

                YiiBase::getPathOfAlias('webroot.files').'/X TRILE1.JPG'); // An image file!!!

    if ($thumb) print '<img src="'.$thumb.'" alt=""/>';

    else print "bad";


  • Invoke your action:

    first time, the api will create the image and caches in the specified dir

    rest, simply returns the cached images so… it’s very fast

  • Pendant things:

    • api for purgue the cached images

    • do as many actions as Drupal ImageCache???? :P

    • open a serious project instead of a forum post



Will try today/tomorrow.

Ok, I’ve created the extension for my initial release in Yii extensions page

I’m using it in a small project just to generate all kind of scaleAndCrop thumbs, and at the moment I have no complains… :lol:



Is pretty easy to use also… you can just drop it in the vendor dir

phpthumb looks ok but it’s much more like ImageAPI, not ImageCache.

Also there are two issues:

— No imagick support yet.

— Configuration is made by editing library source file.

Documentation page here is a bit messy.

What can be improved:

  1. Use preset name to form target path to save images like it’s done in Drupal. It will allow to have two and more thumbnails for the same named image.

  2. Current method and properties names are a bit confusing.

  3. How can I regenerate already generated image?

thx to both for your comments… I’ll take a time to improve the library and after thinking the weekend (spent the whole sunday with a big hangover because of that :P ), I’ll implement the three ways to access the component:

  • tradicional library: that is calling the methods, as any class lib

  • defining presets at app config and calling for createUrl/Path giving the preset and putting the result image in an img tag, in that definition will come the operations over the image, the path to save them, etc

  • as samdark asked, I’ll implement the ImageCache strategy, programming an Action that based on some parameter (preset) will return the image with the preset applied

on the other hand, I’ll implement ImageMagik and of course “purgue” for the caches, lastly, I’ll add more operations for using with presets

anyway… keep in mind that this is a first release so… be patient! (and thanks for testing)



Hey, nice work :)

It’d be great to see the functionality from the Drupal module imagecache_actions too - it has some nice extras such as watermarking, text overlays etc.



I’ve added a function:

public function createAbsoluteUrl($presetName, $file) {

	return Yii::app()->getRequest()->getHostInfo().$this->createUrl($presetName, $file);


I used it in a RSS feed.

it’s very nice extension.

buat i’ve an error from runtime.

the message is like this

2011/09/18 15:40:11 [error] [php] imagecolorsforindex() [<a href=‘function.imagecolorsforindex’>function.imagecolorsforindex</a>]: Color index 99 out of range (C:\xampp\htdocs\protected\extensions\imageapi\GDToolkit.php:106)

i want to know, how it happen.

and how can i fixed it.