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 topath
Usage ¶
Move extension to
/protected/extensions/resizer/
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.
Check for modification and more...
Well i have some questions, as i was doing similar approach recently.
To pmaselkowski
Hi, pmaselkowski
filesize($original_image)
) to cache key.I will add test page in future if community will be interested.
Regards, Bogdan
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.
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.
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.
It works on some images but not others.. why is there inconsistencies?
Hey there,
I realized that sometimes it works on images (even images in subfolders) but sometimes it doesn't work for others (prints out a cannot find image)
Please help,
Thank you
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.