thumbsgen and Kohana Image Library together (extends an extension)

You are viewing revision #2 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.

« previous (#1)

In this wiki I will show you how to extends an extension and/or how can two or more extensions work together.

Actually I show you a real example that I encountered.

In the last year I had created a component that generates thumbnails see this extension here

The problem is that uses native PHP functions and the generated thumnails have low quality.

Using Kohana Image Library (or another) in this extension we can improve the quality and solve this problem.

How to do that ?

1) Follow the instructions both of two extensions. 2) create a php file (either inside thumbsgen folder or another one) that contains this code

$ThumbsGen = 'ThumbsGen';
Yii::setPathOfAlias($ThumbsGen, dirname(__FILE__)); //if you have created this file into another folder change this line accordingly
Yii::import($ThumbsGen . '.ThumbsGen');

class ThumbsGenHQ extends ThumbsGen {

    //overriden method
    function createthumb($source, $dest) {
        
        list($img_width, $img_height, $img_type) = getimagesize($source);

        if ($this->scaleResize !== null) {
            $thumb_w = $img_width * $this->scaleResize;
            $thumb_h = $img_height * $this->scaleResize;
        } else {
            if ($this->thumbWidth === null && $this->thumbHeight === null) {
                throw new CException('cannot set zero both of width and height for thumbnails');
            } else if ($this->thumbHeight === null) {
                $thumb_w = $this->thumbWidth;
                $thumb_h = $this->thumbWidth * ($img_height / $img_width);
            } else if ($this->thumbWidth === null) {
                $thumb_h = $this->thumbHeight;
                $thumb_w = $this->thumbHeight * ($img_width / $img_height);
            } else {
                $thumb_w = $this->thumbWidth;
                $thumb_h = $this->thumbHeight;
            }
        }

        //use another library (Kohana Image Library)
        $image = Yii::app()->image->load($source);
        $image->resize($thumb_w, $thumb_h)->quality(100); 
        $image->save($dest);
        
        return true;
    }

}

modify the step 3 of thumbgen instructions to install the extension application.extensions.ThumbsGen.ThumbsGen to application.extensions.ThumbsGen.ThumbsGenHQ

For example

'ThumbsGen' => array(
                  'class' => 'application.extensions.ThumbsGen.ThumbsGenHQ',
            ),

OR

Yii::app()->setComponents(array('ThumbsGen' => array(
                'class' => 'ext.ThumbsGen.ThumbsGenHQ',
            )));

Now you can use the extended extension ;)