Yii 1.1: Image resize on the fly



  • Yii based application
  • Enabled path UrlFormat for UrlManager
  • All rewrites rules shout be written (.htaccess or any other conf)
  • Image extension for Yii installed ( http://www.yiiframework.com/extension/image/ )

How it’s works.

For example have folder with image resources which are public accessible


Ordinary image displaying would be

<?php echo CHtml::image("/resoures/images/photos/album1/gh53.jpg"); ?>

If we want to get thumbnail of this picture we just have to change a bit url

<?php echo CHtml::image("/resources/thumbs/images/photos/album1/gh53.jpg_200x300.jpg"); ?>

Pretty simple? Isn't it?


To get this working we have to create new Controller with id resources and here is its code.

class ResourcesController extends Controller {
    public function actionThumbs() {
        $request = str_replace(DIRECTORY_SEPARATOR . 'thumbs', '', Yii::app()->request->requestUri);
        $resourcesPath = Yii::getPathOfAlias('webroot') . $request;
        $targetPath = Yii::getPathOfAlias('webroot') . Yii::app()->request->requestUri;
        if (preg_match('/_(\d+)x(\d+).*\.(jpg|jpeg|png|gif)/i', $resourcesPath, $matches)) {
            if (!isset($matches[0]) || !isset($matches[1]) || !isset($matches[2]) || !isset($matches[3]))
                throw new CHttpException(400, 'Non valid params');
            if (!$matches[1] || !$matches[2]) {
                throw new CHttpException(400, 'Invalid dimensions');
            $originalFile = str_replace($matches[0], '', $resourcesPath);
            if (!file_exists($originalFile))
                throw new CHttpException(404, 'File not found');
            $dirname = dirname($targetPath);
            if (!is_dir($dirname))
                mkdir($dirname, 0775, true);
            $image = Yii::app()->image->load($originalFile);
            $image->resize($matches[1], $matches[2]);
            if ($image->save($targetPath)) {
                if (Yii::app()->request->urlReferrer != Yii::app()->request->requestUri)
            throw new CHttpException(500, 'Server error');
        } else {
            throw new CHttpException(400, 'Wrong params');

What was it?

After first attempt to thumbnail /resources/thumbs/images/photos/album1/gh53.jpg_200x300.jpg — thumb action will be triggered because file won’t found on disk.

Script will try to find location of the original source file here /resources/images/photos/album1/gh53.jpg

If source file exists it will be resized and stored here /resources/thumbs/images/photos/album1/gh53.jpg_200x300.jpg

Then script will refresh the url page and after that browser will open newly created thumbnail file directly from disk!

Quite easy!

You can extend this script to support only list of dimensions to avoid disk overflow.

That is all.

Comments are appreciated. Thanks.

PS If you are using Nginx and you want to encrease speed of this script you could use

header("X-Accel-Redirect: /resources/images****");

instead of refresh.

Total 4 comments

#18170 report it
yiqing95 at 2014/09/19 03:32am
yii2 way

nice approach! any possible can this be settled using yii2?

#10656 report it
Maciej Liżewski at 2012/11/12 04:58am
nice approach

nice approach because it uses mod_rewrite default configuration that redirects all request to missing files to front controller. Because of that it is quite efficient and do not require many changes :)

#10652 report it
Tpoxa at 2012/11/11 12:28pm
Iwi module

It doesn't matter. Because my article describes another aspect of this topic.

#10651 report it
kernel32ddl at 2012/11/11 12:21pm

Or you can use Iwi module ;-)

Leave a comment

Please to leave your comment.

Write new article
  • Written by: Tpoxa
  • Category: How-tos
  • Yii Version: 1.1
  • Votes: +4 / -1
  • Viewed: 23,756 times
  • Created on: Nov 11, 2012
  • Last updated: Nov 11, 2012
  • Tags: yii, image-resize