Yii 2 class for working with Imagick.
Run the following command
$ composer require tpmanc/imagick "*"
or add
$ "tpmanc/imagick": "*"
to the require section of your composer.json
file.
Original image:
$img = Imagick::open('./image.jpg');
$img->getWidth();
$img->getHeight();
Imagick::open('./image.jpg')->resize(400, 300)->saveTo('./resized.jpg');
Imagick::open('./image.jpg')->resize(400, false)->saveTo('./resized.jpg');
Imagick::open('./image.jpg')->thumb(200, 200)->saveTo('./thumb.jpg');
$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
Imagick::open('./image.jpg')->flip()->saveTo('./result.jpg');
// horizontal
Imagick::open('./image.jpg')->flop()->saveTo('./result.jpg');
$xStart = 0;
$yStart = 0;
$xEnd = 150;
$yEnd = 150;
Imagick::open('./image.jpg')->crop($xStart, $yStart, $xEnd, $yEnd)->saveTo('./result.jpg');
$radius = 8;
$delta = 5;
Imagick::open('./image.jpg')->blur($radius, $delta)->saveTo('./result.jpg');
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');
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');
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');