Easy Thumbnail Image Helper for Yii2
Yii2 helper for creating and caching thumbnails on real time., (*1)
Installation
The preferred way to install this extension is through composer., (*2)
php composer.phar require "assayer-pro/yii2-easy-thumbnail-image-helper" "*"
or add, (*3)
"assayer-pro/yii2-easy-thumbnail-image-helper" : "*"
to the require section of your application's composer.json
file., (*4)
- Add a new component in
components
section of your application's configuration file (optional), for example:
'components' => [
'thumbnail' => [
'class' => 'assayerpro\thumbnail\EasyThumbnailImage',
'cacheAlias' => 'assets/gallery_thumbnails',
'quality' => 90,
],
],
It is necessary if you want to set global helper's settings for the application., (*5)
Usage
For example:, (*6)
use assayerpro\thumbnail\EasyThumbnailImage;
echo Yii::$app->thumbnail->thumbnailImg(
$model->pictureFile,
50,
50,
EasyThumbnailImage::THUMBNAIL_OUTBOUND,
['alt' => $model->pictureName]
);
or, (*7)
use assayerpro\thumbnail\EasyThumbnailImage;
echo Yii::$app->thumbnail->thumbnailImg(
'http://...',
50,
50,
EasyThumbnailImage::THUMBNAIL_OUTBOUND,
);
If one of thumbnail dimensions is set to null, another one is calculated automatically based on aspect ratio of original image. Note that calculated thumbnail dimension may vary depending on the source image in this case., (*8)
use assayerpro\thumbnail\EasyThumbnailImage;
echo Yii::$app->thumbnail->thumbnailImg(
$model->pictureFile,
50,
null
);
If both dimensions are specified, resulting thumbnail would be exactly the width and height specified. How it's achieved depends on the mode., (*9)
For other functions please see the source code., (*10)
If you want to handle errors that appear while converting to thumbnail by yourself, please make your own class and inherit it from EasyThumbnailImage. In your class replace only protected method errorHandler. For example, (*11)
class ThumbHelper extends \assayerpro\thumbnail\EasyThumbnailImage
{
protected static function errorHandler($error, $filename)
{
if ($error instanceof \assayerpro\thumbnail\FileNotFoundException) {
return \yii\helpers\Html::img('@web/images/notfound.png');
} else {
$filename = basename($filename);
return \yii\helpers\Html::a($filename,"@web/files/$filename");
}
}
}