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
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
Attach the component in your config file:
'components' => [
'thumbnail' => [
'class' => 'sadovojav\image\Thumbnail',
],
],
basePath
= null
- Base pathprefixPath
= null
- Prefix pathcachePath
= @runtime/thumbnails
- Cache path aliascacheExpire
= 604800
- Cache expire timeoptions
- Other options (placeholder/quality/additional compression)'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
]
]
textSize - does't support on PLACEHOLDER_TYPE_URL
echo Yii::$app->thumbnail->img($file, $params, $options);
This method returns Html::img()
$file
required - Image file path$params
required - Image manipulation methods. See Methods$options
- options for Html::img()<?= Yii::$app->thumbnail->img(IMAGE_SRC, [
'thumbnail' => [
'width' => 320,
'height' => 230,
],
'placeholder' => [
'width' => 320,
'height' => 230
]
]); ?>
echo Yii::$app->thumbnail->url($file, $params);
This method returns cache image url
$file
required - Image file path$params
- Image manipulation methods. See Methods<?= Yii::$app->thumbnail->url(IMAGE_SRC, [
'thumbnail' => [
'width' => 320,
'height' => 230,
],
'placeholder' => [
'width' => 320,
'height' => 230
]
]); ?>
echo Yii::$app->thumbnail->img($file, $params, $options);
This method returns Html::img()
$file
required - must to be Null
$params
required - Image manipulation methods. See Methods$options
- options for Html::img()<?= Yii::$app->thumbnail->img(null, [
'placeholder' => [
'width' => 320,
'height' => 230
]
]); ?>
echo Yii::$app->thumbnail->url($file, $params, $options);
This method returns path to placeholder image
$file
required - must to be Null
$params
required - Image manipulation methods. See Methods$options
- options for Html::img()<?= Yii::$app->thumbnail->url(null, [
'placeholder' => [
'width' => 320,
'height' => 230
]
]); ?>
'resize' => [
'width' => 320,
'height' => 200
]
width
required - New widthheight
required - New height'crop' => [
'width' => 250,
'height' => 200,
]
width
required - New widthheight
required - New heightx
= 0
- X start crop positiony
= 0
- Y start crop position'thumbnail' => [
'width' => 450,
'height' => 250,
]
width
required - New widthheight
required - New heightmode
= THUMBNAIL_OUTBOUND
- Thumbnail mode THUMBNAIL_OUTBOUND
or THUMBNAIL_INSET
'placeholder' => [
'width' => 450,
'height' => 250,
]
This method return image placeholder if image file doesn't exist.
width
required - Placeholder image widthheight
required - Placeholder image heightbackgroundColor
= #f5f5f5
- Background colortextColor
= #cdcdcd
- Text colortext
= No image
- Text'watermark' => [
'image' => IMAGE_SRC
'posX' => 0,
'posY' => 0,
'width' => 50,
'height' => 50
]
image
required - watermark pathposX
required - X/-X watermark positionposY
required - Y/-Y watermark positionwidth
- Watermark widthheight
- Watermark heightmode
= THUMBNAIL_OUTBOUND
- Thumbnail mode THUMBNAIL_OUTBOUND
or THUMBNAIL_INSET
'compress' => true