resizer

Simple images resizer extension
6 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 4 comments

#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