Yii 2 Imagick
Yii 2 class for working with Imagick., (*1)
Install via Composer
Run the following command, (*2)
$ composer require tpmanc/yii2-imagick "*"
or add, (*3)
$ "tpmanc/yii2-imagick": "*"
to the require section of your composer.json
file., (*4)
Original image:, (*5)
, (*6)
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');
, (*7)
Create thumbnail
Imagick::open('./image.jpg')->thumb(200, 200)->saveTo('./thumb.jpg');
, (*8)
Add border
$width = 5;
$color = '#000'
Imagick::open('./image.jpg')->border($width, $color)->saveTo('./result.jpg');
, (*9)
$width = 10;
$color = '#A91AD4'
Imagick::open('./image.jpg')->border($width, $color)->saveTo('./result.jpg');
, (*10)
Vertical and horizontal mirror image
// vertical
Imagick::open('./image.jpg')->flip()->saveTo('./result.jpg');
// horizontal
Imagick::open('./image.jpg')->flop()->saveTo('./result.jpg');
, (*11)
, (*12)
Crop
$xStart = 0;
$yStart = 0;
$xEnd = 150;
$yEnd = 150;
Imagick::open('./image.jpg')->crop($xStart, $yStart, $xEnd, $yEnd)->saveTo('./result.jpg');
, (*13)
Blur
$radius = 8;
$delta = 5;
Imagick::open('./image.jpg')->blur($radius, $delta)->saveTo('./result.jpg');
, (*14)
Watermark
Set watermark position
Use $xPosition
and $yPosition
to set watermark position., (*15)
$xPosition
should be 'left', 'right' or 'center'; $yPosition
should be 'top', 'bottom' or 'center'., (*16)
$xPosition = 'left';
$yPosition = 'top';
Imagick::open('./image.jpg')->watermark('./watermark.png'), $xPosition, $yPosition)->saveTo('./result.jpg');
, (*17)
$xPosition = 'right';
$yPosition = 'center';
Imagick::open('./image.jpg')->watermark('./watermark.png'), $xPosition, $yPosition)->saveTo('./result.jpg');
, (*18)
Set watermark size
Use $xSize
and $ySize
to set watermark size. Valid values:, (*19)
-
Number: $xSize = 100;
, $ySize = 50
, (*20)
-
Percent of parent: $xSize = '100%';
, $ySize = '50%'
, (*21)
-
'auto'
to save proportion: $xSize = '100%';
, $ySize = 'auto'
, (*22)
-
false
: $xSize = 100;
, $ySize = false
, (*23)
$xPosition = 'center';
$yPosition = 'center';
$xSize = '100%';
$ySize = 'auto';
Imagick::open('./image.jpg')->watermark('./watermark.png'), $xPosition, $yPosition, $xSize, $ySize)->saveTo('./result.jpg');
, (*24)
$xPosition = 'center';
$yPosition = 'center';
$xSize = '100%';
$ySize = '100%';
Imagick::open('./image.jpg')->watermark('./watermark.png'), $xPosition, $yPosition, $xSize, $ySize)->saveTo('./result.jpg');
, (*25)
Set watermark offset
Use $xOffset
and $yOffset
to set offset from parent image border., (*26)
$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');
, (*27)