assayer-pro / yii2-easy-thumbnail-image-helper
Yii2 helper for creating and caching thumbnails on real time
Installs: 34
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 16
Open Issues: 0
Type:yii2-extension
Requires
- yiisoft/yii2: ~2.0.13
- yiisoft/yii2-httpclient: ~2.0.0
- yiisoft/yii2-imagine: *
This package is auto-updated.
Last update: 2024-12-14 05:49:44 UTC
README
Yii2 helper for creating and caching thumbnails on real time.
Installation
The preferred way to install this extension is through composer.
- Either run
php composer.phar require "assayer-pro/yii2-easy-thumbnail-image-helper" "*"
or add
"assayer-pro/yii2-easy-thumbnail-image-helper" : "*"
to the require section of your application's composer.json
file.
- 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.
Usage
For example:
use assayerpro\thumbnail\EasyThumbnailImage; echo Yii::$app->thumbnail->thumbnailImg( $model->pictureFile, 50, 50, EasyThumbnailImage::THUMBNAIL_OUTBOUND, ['alt' => $model->pictureName] );
or
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.
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.
For other functions please see the source code.
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
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"); } } }