assayer-pro/yii2-easy-thumbnail-image-helper

Yii2 helper for creating and caching thumbnails on real time

1.2.0 2018-03-08 07:27 UTC

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");
        }
    }
}