tpmanc/yii2-imagick

用于操作 Imagick 的 Yii 2 类

1.3.2 2019-12-10 08:54 UTC

This package is not auto-updated.

Last update: 2024-09-27 09:25:25 UTC


README

用于操作 Imagick 的 Yii 2 类。

通过 Composer 安装

运行以下命令

$ composer require tpmanc/yii2-imagick "*"

或在您的 composer.json 文件的 require 部分添加

$ "tpmanc/yii2-imagick": "*"

to the require section of your composer.json file.

原始图像

"Original"

获取大小

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

"Resize"

创建缩略图

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

"Thumb"

添加边框

$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
Imagick::open('./image.jpg')->flip()->saveTo('./result.jpg');
// horizontal
Imagick::open('./image.jpg')->flop()->saveTo('./result.jpg');

"Flip"

"Flop"

裁剪

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

"Crop"

模糊

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

"Blur"

水印

设置水印位置

使用 $xPosition$yPosition 来设置水印位置。

$xPosition 应为 'left', 'right' 或 'center'; $yPosition 应为 'top', 'bottom' 或 '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"

设置水印大小

使用 $xSize$ySize 来设置水印大小。有效值

  • 数字: $xSize = 100;, $ySize = 50

  • 父元素百分比: $xSize = '100%';, $ySize = '50%'

  • 保持比例: 'auto': $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"

设置水印偏移

使用 $xOffset$yOffset 来设置从父图像边框的偏移。

$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"