Yii 1.1: resizer

Simple images resizer extension
8 followers

Why can you need it?

Have an images gallery?
Tired of thinking what images sizes will you need?
Waste your time changing sizes and regenerating images?

This simple extension allows you to forget about this problems.
All you will need is to keep base (original) image.

Resizer extension will generate image whatever size you need, just on fly!

Requirements

You will need:

  • Yii 1.1 or above
  • GD extension
  • Yii urlManager urlFormat must be set to path

Usage

  1. Move extension to /protected/extensions/resizer/

  2. To use this extension you will need some controller. Extension is installed as controller action. Let's assume our controller name is test, and we will use resized action as name.

In your controller you need to add:

public function actions()
{
    return array(
        'resized' => array(
            'class'   => 'ext.resizer.ResizerAction',
            'options' => array(
                // Tmp dir to store cached resized images 
                'cache_dir'   => Yii::getPathOfAlias('webroot') . '/assets/',
 
                // Web root dir to search images from
                'base_dir'    => Yii::getPathOfAlias('webroot') . '/',
            )
        ),
    );
}

Let's assume you have an image http://domain/images/big.png.

To get 100x100 icon, use the following URL :

http://domain/test/resized/100x100/images/big.png

So, as you see, URL structure is:

http://SERVER_NAME/CONTROLLER_ID/ACTION_ID/WIDTHxHEIGHT/PATH_TO_IMAGE

Summary

May have some bugs. Please, report them or ask questions.

Total 5 comments

#14067 report it
Sebastian at 2013/07/16 05:03pm
The code needs some love

First thing: the resizing and caching part works nice. But it needs to be said the images definitely get cropped when aspect ratio is not met by the given dimensions. For me, this is fine. But it behaves contrary to your response. Second thing: I could pull my hair out, when I see you reinventing the wheel in your code. The extension contains much duplicated code, but what makes it worse it that you neglect features like Yiis fantastic routing system, which also knows something like this:

'urlManager' => array(
            ...
                'image/get/<x>x<y>/<image_name>' => 'image/get',
            ),

Repack your CAction as a Controller (with a public function actionGet($x, $y, $image_name)) and you will get a much cleaner API. Plus you can drop semi-exploitable code like

$pcs = explode('/', $q);
unset($pcs[0]);
foreach ($pcs as $k => $piece) {
    if ($piece == '..') {
        unset($pcs[$k]);
    }
}

which imho poses a subtle threat to misconfigured servers. Yii can do this for you and is much preferred to home-brew-methods.

But anyway: nice job. It came into use for me.

#13327 report it
Termit132 at 2013/05/22 11:44am
Fix error with path

Hi. When i tried to load images from default directory of images - it calls "images" an image was loaded perfectly. But when i tried to load image from "images/profile" i get a strange error : Unable to open 'somepath/assets/04c460b1cdecb0fe3357eca3f51283eb_100x100.IMAGES/PROFILE/9' for writing: No such file or directory (mypath/protected/extensions/resizer/index.php:225) (somepath and mypath is for example) So, i go to index.php and find problem in 57 string:

$pcs = explode('.', $img_path);

And i change it to

$pcs = explode('/', $img_path);

And image was loaded successfully. But I'm still grateful for the extension.

#12017 report it
pmaselkowski at 2013/02/20 02:57pm
Re:

I think it would be good idea to add crop resize option. So image could be resized to say, square while keeping pixel aspect ratio. So one could create gallery with thumbs in same size and not distorted.

With cache. Each request actually runs thru Yii, so it's easy to cache and to detect changes. Drawback is that for each thumb php has to be executed. Well, with direct generating images it is more efficient, but way more comlicated.

#12014 report it
ASAP at 2013/02/20 12:53pm
To pmaselkowski

Hi, pmaselkowski

  1. Images are cached, of course.
  2. Detect original image change - possible. Not yet done. To do this we can, for example, add image size (filesize($original_image)) to cache key.
  3. If your image is 600x400 and you put size 100x100 you will get image with size 100x100. It will not be cropped, it will be proportionally resized.

I will add test page in future if community will be interested.

Regards, Bogdan

#12013 report it
pmaselkowski at 2013/02/20 12:38pm
Check for modification and more...

Well i have some questions, as i was doing similar approach recently.

  1. I guest it cache images somehow, or not?
  2. If cache, than is there some way to detect original image change?
  3. I guest if my image is 600x400 and i put size 100x100 it will be cropped, not squished, right? :)

Leave a comment

Please to leave your comment.

Create extension