Like the Image module in Drupal, this extension will resize your images on demand :-). If a thumb doesn't exist, the web server's rewrite rules will pass the request to Yii which in turn hands it off to ImageCache to dynamically generate the file.
WIP...
The preferred way to install this helper is through composer.
Either run
php composer.phar require "iutbay/yii2-imagecache" "*"
or add
"iutbay/yii2-imagecache" : "*"
to the require section of your application's composer.json
file.
You should :
ThumbAction
in one of your controller.You need to add ThumbAction
in one of your controller so that imageCache can handle requests to missing thumbs and create them on demand. You could use site
controller :
class SiteController extends Controller
{
...
public function actions()
{
return [
...
'thumb' => 'iutbay\yii2imagecache\ThumbAction',
...
];
}
...
}
You should add imageCache component in your application configuration :
$config = [
'components' => [
...
'imageCache' => [
'class' => 'iutbay\yii2imagecache\ImageCache',
'sourcePath' => '@app/web/images',
'sourceUrl' => '@web/images',
//'thumbsPath' => '@app/web/thumbs',
//'thumbsUrl' => '@web/thumbs',
//'sizes' => [
// 'thumb' => [150, 150],
// 'medium' => [300, 300],
// 'large' => [600, 600],
//],
],
...
],
];
You should enable pretty urls :
http://www.yiiframework.com/doc-2.0/guide-start-installation.html#configuring-web-servers
You should modify your urlManager configuration :
$config = [
'components' => [
...
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
...
'thumbs/<path:.*>' => 'site/thumb',
...
],
],
...
],
];
<?= Yii::$app->imageCache->thumb('@web/images/2014/test.jpg') ?>
// <img src="/your-app/thumbs/2014/test_thumb.jpg" alt="">
<?= Yii::$app->imageCache->thumb('@web/images/2014/test.jpg', 'medium') ?>
// <img src="/your-app/thumbs/2014/test_medium.jpg" alt="">
<?= Yii::$app->imageCache->thumb('@web/images/2014/test.jpg', 'medium', ['class'=>'img']) ?>
// <img src="/your-app/thumbs/2014/test_medium.jpg" alt="" class="img">
Comments