yiicod/yii2-easyimage

Yii 2 框架扩展,用于实时创建和缓存缩略图。

1.0.0 2018-02-18 18:21 UTC

This package is auto-updated.

Last update: 2024-08-27 00:24:01 UTC


README

Latest Stable Version Total Downloads Scrutinizer Code QualityCode Climate

您不需要为项目中的图像创建许多类型的缩略图。您可以直接在 View 中创建缩略图。缩略图将自动缓存。这很简单!

特性

  • 易于使用
  • 支持 ImagickGmagickGD2
  • 自动图像缓存
  • 缓存排序到子目录

安装和配置

将以下内容添加到配置文件的 components 部分

'easyimage' => [
    'class' => 'yiicod\easyimage\EasyImage',
    'webrootAlias' => '@frontend/web',
    'cachePath' => '/uploads/easyimage/',
    'imageOptions' => [
        'quality' => 100
    ],
],

参数

  • string webrootAlias webroot 文件夹路径
  • string cachePath webroot 文件夹中的缓存目录路径
  • array imageOptions 数组,包含用于保存图像的默认输出图像选项,Imagine 将使用这些选项

用法

Yii::$app->easyimage->getUrl($file, $params, $absolute); //Get cached image url
Yii::$app->easyimage->getPath($file, $params); //Get cached image path

Note: you can get access to the component using static method EasyImage::getInstance()

参数

  • string $file 必需 - 图像文件路径
  • array $params 图像工具参数 默认:[]
  • mixed $absolute 获取绝对(true)URL还是不获取(false)。将在 Url::to() 辅助方法中使用

可用的工具和参数

'crop' => [
    'width' => 200, //Required
    'height' => 200, //Required
    'offset_x' => 100,
    'offset_y' => 100,
],
'flip' => [
    'axis' => 'vertical', //Required available 'vertical' or 'horizontal'
],
'resize' => [
    'width' => 200, //Required
    'height' => 200, //Required
],
'scale' => [
    'width' => 200, //Required
    'height' => 200, //Required
],
'rotate' => [
    'angle' => 45 //Required
],
'thumbnail' => [
    'width' => 100, //Required one of dimensions
    'height' => 100, //Required one of dimensions
],
'watermark' => [
    'image' => $file, //Required
    'offset_x' => 100,
    'offset_y' => 100,
],
'background' => [
    'color' => '#000', //Required
    'alpha' => 0 //Background color's alpha
],

工具

您可以在配置中添加额外的工具

'easyimage' => [
    'class' => 'yiicod\easyimage\EasyImage',
    'webrootAlias' => '@frontend/web',
    'cachePath' => '/uploads/easyimage/',
    'tools' => [
        'crop' => Crop::class,
    ],
],
  • 'crop' - 在 getUrl() 或 getPath() 参数中使用时将使用的工具名称
  • Crop::class - 必须实现 yiicod\easyimage\base\ToolInterface 的工具类名称