For example have folder with image resources which are public accessible
/resources/images/avatars/* /resources/images/posters/* /resources/images/photos/* /resources/images/photos/album1/*
Ordinary image displaying would be
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
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) $this->refresh(); } throw new CHttpException(500, 'Server error'); } else { throw new CHttpException(400, 'Wrong params'); } } }
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 3 comments
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 :)
It doesn't matter. Because my article describes another aspect of this topic.
Or you can use Iwi module ;-)
Leave a comment
Please login to leave your comment.