sadovojav/yii2-image-thumbnail

此软件包最新版本(v3.1)没有可用的许可信息。

Yii2图片缩略图组件

安装次数31,930

依赖项: 3

建议者: 0

安全: 0

星标: 21

关注者: 7

分支: 12

公开问题: 4

类型:yii2-extension

v3.1 2021-01-24 15:47 UTC

This package is auto-updated.

Last update: 2024-09-09 00:11:56 UTC


README

使用Imagine创建图像缩略图。缩略图创建和缓存自动完成。它允许您使用https://placeholder.com/、holder.js或Imagine创建占位符。

特性

  • 易于使用
  • 使用Imagine
  • TinyPng压缩
  • 自动缩略图缓存
  • 缓存排序到子目录
  • 从URL缓存占位符(placeholdit.imgix.net)
  • 使用placeholdit.imgix.net

安装

Composer

安装此扩展的首选方式是通过Composer

运行php composer.phar require sadovojav/yii2-image-thumbnail "dev-master"

或将"sadovojav/yii2-image-thumbnail": "dev-master"添加到您的composer.json文件的require部分

配置

在您的配置文件中附加组件

'components' => [
    'thumbnail' => [
        'class' => 'sadovojav\image\Thumbnail',
    ],
],

参数

  • string basePath = null - 基础路径
  • string prefixPath = null - 前缀路径
  • string cachePath = @runtime/thumbnails - 缓存路径别名
  • integer cacheExpire = 604800 - 缓存过期时间
  • array options - 其他选项(占位符/质量/额外压缩)

默认选项

'options' => [
    'placeholder' => [
        'type' => sadovojav\image\Thumbnail::PLACEHOLDER_TYPE_URL,
        'backgroundColor' => '#f5f5f5',
        'textColor' => '#cdcdcd',
        'textSize' => 30,
        'text' => 'No image'
    ],
    'quality' => 92,
    'format' => null, //webp, jpg, png
    'tinyPng' => [
        'apiKey' => null
    ]
]

注意

textSize - 不支持在PLACEHOLDER_TYPE_URL上

占位符类型

    1. sadovojav\image\Thumbnail::PLACEHOLDER_TYPE_URL - 通过URL获取占位符
    1. sadovojav\image\Thumbnail::PLACEHOLDER_TYPE_IMAGINE - 使用Imagine创建占位符

使用

获取缓存图像

echo Yii::$app->thumbnail->img($file, $params, $options);

此方法返回Html::img()

参数

  • string $file 必需 - 图像文件路径
  • array $params 必需 - 图像处理方法。见方法
  • array $options - Html::img()的选项

例如

<?= Yii::$app->thumbnail->img(IMAGE_SRC, [
    'thumbnail' => [
        'width' => 320,
        'height' => 230,
    ],
    'placeholder' => [
        'width' => 320,
        'height' => 230
    ]
]); ?>

获取缓存图像URL

echo Yii::$app->thumbnail->url($file, $params);

此方法返回缓存图像URL

参数

  • string $file 必需 - 图像文件路径
  • array $params - 图像处理方法。见方法

例如

<?= Yii::$app->thumbnail->url(IMAGE_SRC, [
    'thumbnail' => [
        'width' => 320,
        'height' => 230,
    ],
    'placeholder' => [
        'width' => 320,
        'height' => 230
    ]
]); ?>

获取占位符图像

echo Yii::$app->thumbnail->img($file, $params, $options);

此方法返回Html::img()

参数

  • string $file 必需 - 必须为Null
  • array $params 必需 - 图像处理方法。见方法
  • array $options - Html::img()的选项

例如

<?= Yii::$app->thumbnail->img(null, [
    'placeholder' => [
        'width' => 320,
        'height' => 230
    ]
]); ?>

获取占位符URL

echo Yii::$app->thumbnail->url($file, $params, $options);

此方法返回占位符图像的路径

参数

  • string $file 必需 - 必须为Null
  • array $params 必需 - 图像处理方法。见方法
  • array $options - Html::img()的选项

例如

<?= Yii::$app->thumbnail->url(null, [
    'placeholder' => [
        'width' => 320,
        'height' => 230
    ]
]); ?>

方法

调整大小

'resize' => [
    'width' => 320,
    'height' => 200
]

参数

  • integer width 必需 - 新宽度
  • integer height 必需 - 新高度

裁剪

'crop' => [
    'width' => 250,
    'height' => 200,
]

参数

  • integer width 必需 - 新宽度
  • integer height 必需 - 新高度
  • integer x = 0 - X开始裁剪位置
  • integer y = 0 - Y开始裁剪位置

缩略图

'thumbnail' => [
    'width' => 450,
    'height' => 250,
]

参数

  • integer width 必需 - 新宽度
  • integer height 必需 - 新高度
  • string mode = THUMBNAIL_OUTBOUND - 缩略图模式 THUMBNAIL_OUTBOUNDTHUMBNAIL_INSET

占位符

'placeholder' => [
    'width' => 450,
    'height' => 250,
]

如果图像文件不存在,此方法返回图像占位符。

参数

  • integer width 必需 - 占位符图像宽度
  • integer height 必需 - 占位符图像高度
  • string backgroundColor = #f5f5f5 - 背景颜色
  • string textColor = #cdcdcd - 文本颜色
  • string text = No image - 文本

水印

'watermark' => [
    'image' => IMAGE_SRC
    'posX' => 0,
    'posY' => 0,
    'width' => 50,
    'height' => 50
]

参数

  • string image 必需 - 水印路径
  • integer posX 必需 - X/-X水印位置
  • integer posY 必需 - Y/-Y水印位置
  • integer width - 水印宽度
  • integer height - 水印高度
  • string mode = THUMBNAIL_OUTBOUND - 缩略图模式 THUMBNAIL_OUTBOUNDTHUMBNAIL_INSET

压缩(TinyPng)

'compress' => true