andrewdanilov/yii2-thumbs

图像缩放和缓存

安装次数: 93

依赖项: 0

建议者: 0

安全: 0

星星: 3

关注者: 2

分支: 1

类型:yii2-extension

1.1.0 2023-06-20 22:03 UTC

This package is auto-updated.

Last update: 2024-09-21 00:54:00 UTC


README

提供用于缩放和缓存图像的小部件类

安装

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

运行以下命令之一:

composer require andrewdanilov/yii2-thumbs "~1.1.0"

或添加

"andrewdanilov/yii2-thumbs": "~1.1.0"

到您的 composer.json 文件的 require 部分。

使用方法

$src = \andrewdanilov\thumbs\Thumb::widget([
    'image' => '/images/img.png', // Image to resize and cache relative to base site uri
    'sizes' => '100x100', // Optional. Result image sizes. String devided with 'x' or array with 'width' and 'height' keys. Default is '100x100'
    'zc' => true, // Optional. Zoom and crop. Default is true.
    'quality' => 90, // Optional. Jpeg quality from 1 to 100. Default is 90
    'backgroundColor' => 'transparent', // Optional. Values like 'transparent', 'FFF', '000000'. Default is 'transparent'
    'noImageUri' => '/images/noimg.png', // Optional. Path to image to use for empty or absent images relative to base site uri
    'cachePath' => '/assets/thumbs/', // Optional. Path to store cached images relative to base site uri. Default is '/assets/thumbs/'
    'cacheTime' => 604800, // Optional. Time to chache in seconds. Default is 604800
]);
echo \yii\helpers\Html::img($src);

sizes 参数中,您可以指定宽度和高度参数中的一个或两个。您可以使用 'x' 分隔符以字符串形式描述,或者使用具有 widthheight 键的数组形式描述

// width x height string notation
$sizes = '400x200';
$sizes = '400x';
$sizes = 'x200';
// array notation
$sizes = ['width' => 400, 'height' => 200];
$sizes = ['width' => 400];
$sizes = ['height' => 200];

如果指定了两个尺寸,则根据 zc 参数的值,结果图像将被裁剪(zc = true)或填充(zc = false),填充颜色由 backgroundColor 参数指定。

noImageUri 参数中的 URI 必须存在。如果您没有,您可以从 /vendor/andrewdanilov/yii2-thumbs/src/web/images/noimage.png 复制到您的位置。

如果 cachePath 参数的路径不存在,它将被创建。