Skip to content

tpmanc/yii2-imagick

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

"Original"

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');

"Resize"

Create thumbnail

Imagick::open('./image.jpg')->thumb(200, 200)->saveTo('./thumb.jpg');

"Thumb"

Add border

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

"Resize"

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

"Resize"

Vertical and horizontal mirror image

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

"Flip"

"Flop"

Crop

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

"Crop"

Blur

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

"Blur"

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');

"Watermark"

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

"Watermark"

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');

"Watermark"

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

"Watermark"

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');

"Watermark"