yurkinx / yii2-image
使用 Kohana Image Library 进行图像处理的 Yii2 扩展。
v1.3
2019-01-25 01:01 UTC
Requires
- php: >=5.4.0
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-09-14 13:41:13 UTC
README
这是一个简单易用的 Yii2 框架扩展,使用强大的 Kohana Image Library 进行图像处理。灵感来源于旧的 yii 扩展 https://yiiframework.cn/extension/image/ 和 Kohana Image Library https://github.com/kohana/image
安装
作为 composer 包 安装
使用此方法获取持续更新。
composer require yurkinx/yii2-image
或者将依赖项包含在 composer.json
文件中
{ "require": { "yurkinx/yii2-image": "^1.2" } }
配置
在配置文件中
/config/web.php
添加图像组件
'components' => array(
...
'image' => array(
'class' => 'yii\image\ImageDriver',
'driver' => 'GD', //GD or Imagick
),
)
用法
$file=Yii::getAlias('@app/pass/to/file'); $image=Yii::$app->image->load($file); header("Content-Type: image/png"); echo $image->resize($width,$height)->rotate(30)->render();
直接支持 Kohana Image Library 的方法
$image->resize($width = NULL, $height = NULL, $master = NULL); $image->crop($width, $height, $offset_x = NULL, $offset_y = NULL); $image->sharpen($amount); $image->rotate($degrees); $image->save($file = NULL, $quality = 100); $image->render($type = NULL, $quality = 100); $image->reflection($height = NULL, $opacity = 100, $fade_in = FALSE); $image->flip($direction); $image->background($color, $opacity = 100); $image->watermark(Image $watermark, $offset_x = NULL, $offset_y = NULL, $opacity = 100);
使用带有缩放约束的缩放
$image->resize($width, $height, \yii\image\drivers\Image::HEIGHT); $image->resize($width, $height, \yii\image\drivers\Image::ADAPT)->background('#fff');
使用带有缩放约束和最佳质量输出的缩放 [仅适用于 Imagick 驱动器]
使用 1 以获得最佳速度和较低质量,使用 100 以获得最佳质量和较低速度。目前仅支持 1 和 100
$image->resize($width, NULL, \yii\image\drivers\Image::WIDTH, 100);
可能的缩放约束
// Resizing constraints ($master) const NONE = 0x01; const WIDTH = 0x02; const HEIGHT = 0x03; const AUTO = 0x04; const INVERSE = 0x05; const PRECISE = 0x06; const ADAPT = 0x07; const CROP = 0x08;
使用翻转方向进行翻转
// Flipping directions ($direction) $image->flip(\yii\image\drivers\Image::HORIZONTAL);
可能的翻转方向
const HORIZONTAL = 0x11; const VERTICAL = 0x12;