yii2-imagick
yii2-imagick copied to clipboard
Class for working with Imagick
Yii 2 Imagick
Yii 2 class for working with Imagick.
Install via Composer
Run the following command
$ composer require tpmanc/yii2-imagick "*"
or add
$ "tpmanc/yii2-imagick": "*"
to the require section of your composer.json file.
Original image:

Get size
$img = Imagick::open('./image.jpg');
$img->getWidth();
$img->getHeight();
Resize image
Imagick::open('./image.jpg')->resize(400, 300)->saveTo('./resized.jpg');
Imagick::open('./image.jpg')->resize(400, false)->saveTo('./resized.jpg');

Create thumbnail
Imagick::open('./image.jpg')->thumb(200, 200)->saveTo('./thumb.jpg');
![]()
Add border
$width = 5;
$color = '#000'
Imagick::open('./image.jpg')->border($width, $color)->saveTo('./result.jpg');

$width = 10;
$color = '#A91AD4'
Imagick::open('./image.jpg')->border($width, $color)->saveTo('./result.jpg');

Vertical and horizontal mirror image
// vertical
Imagick::open('./image.jpg')->flip()->saveTo('./result.jpg');
// horizontal
Imagick::open('./image.jpg')->flop()->saveTo('./result.jpg');


Crop
$xStart = 0;
$yStart = 0;
$xEnd = 150;
$yEnd = 150;
Imagick::open('./image.jpg')->crop($xStart, $yStart, $xEnd, $yEnd)->saveTo('./result.jpg');

Blur
$radius = 8;
$delta = 5;
Imagick::open('./image.jpg')->blur($radius, $delta)->saveTo('./result.jpg');

Watermark
Set watermark position
Use $xPosition and $yPosition to set watermark position.
$xPosition should be 'left', 'right' or 'center'; $yPosition should be 'top', 'bottom' or 'center'.
$xPosition = 'left';
$yPosition = 'top';
Imagick::open('./image.jpg')->watermark('./watermark.png'), $xPosition, $yPosition)->saveTo('./result.jpg');

$xPosition = 'right';
$yPosition = 'center';
Imagick::open('./image.jpg')->watermark('./watermark.png'), $xPosition, $yPosition)->saveTo('./result.jpg');

Set watermark size
Use $xSize and $ySize to set watermark size. Valid values:
-
Number:
$xSize = 100;,$ySize = 50 -
Percent of parent:
$xSize = '100%';,$ySize = '50%' -
'auto'to save proportion:$xSize = '100%';,$ySize = 'auto' -
false:$xSize = 100;,$ySize = false
$xPosition = 'center';
$yPosition = 'center';
$xSize = '100%';
$ySize = 'auto';
Imagick::open('./image.jpg')->watermark('./watermark.png'), $xPosition, $yPosition, $xSize, $ySize)->saveTo('./result.jpg');

$xPosition = 'center';
$yPosition = 'center';
$xSize = '100%';
$ySize = '100%';
Imagick::open('./image.jpg')->watermark('./watermark.png'), $xPosition, $yPosition, $xSize, $ySize)->saveTo('./result.jpg');

Set watermark offset
Use $xOffset and $yOffset to set offset from parent image border.
$xPosition = 'right';
$yPosition = 'bottom';
$xSize = false;
$ySize = false;
$xOffset = 50;
$yOffset = 50;
Imagick::open('./image.jpg')->watermark('./watermark.png'), $xPosition, $yPosition, $xSize, $ySize, $xOffset, $yOffset)->saveTo('./result.jpg');
