sadovojav / yii2-image-thumbnail

Create image thumbnail Yii2 component
GNU General Public License v3.0
21 stars 12 forks source link

Yii2 image thumbnail

Create image thumbnails use Imagine. Thumbnail created and cached automatically. It allows you to create placeholder with service https://placeholder.com/, holder.js or Imagine

Features:

Installation

Composer

The preferred way to install this extension is through Composer.

Either run php composer.phar require sadovojav/yii2-image-thumbnail "dev-master"

or add "sadovojav/yii2-image-thumbnail": "dev-master" to the require section of your composer.json

Config

Attach the component in your config file:

'components' => [
    'thumbnail' => [
        'class' => 'sadovojav\image\Thumbnail',
    ],
],

Parameters

Default options:

'options' => [
    'placeholder' => [
        'type' => sadovojav\image\Thumbnail::PLACEHOLDER_TYPE_URL,
        'backgroundColor' => '#f5f5f5',
        'textColor' => '#cdcdcd',
        'textSize' => 30,
        'text' => 'No image'
    ],
    'quality' => 92,
    'format' => null, //webp, jpg, png
    'tinyPng' => [
        'apiKey' => null
    ]
]

Note

textSize - does't support on PLACEHOLDER_TYPE_URL

Placeholder type

Using

Get cache image

echo Yii::$app->thumbnail->img($file, $params, $options);

This method returns Html::img()

Parameters

For example:

<?= Yii::$app->thumbnail->img(IMAGE_SRC, [
    'thumbnail' => [
        'width' => 320,
        'height' => 230,
    ],
    'placeholder' => [
        'width' => 320,
        'height' => 230
    ]
]); ?>

Get cache image url

echo Yii::$app->thumbnail->url($file, $params);

This method returns cache image url

Parameters

For example:

<?= Yii::$app->thumbnail->url(IMAGE_SRC, [
    'thumbnail' => [
        'width' => 320,
        'height' => 230,
    ],
    'placeholder' => [
        'width' => 320,
        'height' => 230
    ]
]); ?>

Get placeholder image

echo Yii::$app->thumbnail->img($file, $params, $options);

This method returns Html::img()

Parameters

For example:

<?= Yii::$app->thumbnail->img(null, [
    'placeholder' => [
        'width' => 320,
        'height' => 230
    ]
]); ?>

Get placeholder url

echo Yii::$app->thumbnail->url($file, $params, $options);

This method returns path to placeholder image

Parameters

For example:

<?= Yii::$app->thumbnail->url(null, [
    'placeholder' => [
        'width' => 320,
        'height' => 230
    ]
]); ?>

Method

Resize

'resize' => [
    'width' => 320,
    'height' => 200
]

Parameters

Crop

'crop' => [
    'width' => 250,
    'height' => 200,
]

Parameters

Thumbnail

'thumbnail' => [
    'width' => 450,
    'height' => 250,
]

Parameters

Placeholder

'placeholder' => [
    'width' => 450,
    'height' => 250,
]

This method return image placeholder if image file doesn't exist.

Parameters

Watermark

'watermark' => [
    'image' => IMAGE_SRC
    'posX' => 0,
    'posY' => 0,
    'width' => 50,
    'height' => 50
]

Parameters

Compression (TinyPng)

'compress' => true