maximal/yii2-webp-thumbnailer

基于Imagine的Yii2辅助工具,用于实时创建和缓存WebP缩略图

安装次数: 410

依赖项: 0

建议者: 0

安全性: 0

星标: 4

关注者: 3

分支: 2

开放问题: 0

类型:yii2-extension

v1.0.0 2020-05-07 13:22 UTC

This package is auto-updated.

Last update: 2024-09-18 15:44:36 UTC


README

这是基于PHP WebP缩略图生成器的Yii2辅助工具,允许您在Yii2应用程序中动态生成和缓存WebP图像缩略图。

安装

使用Composer安装此库

php composer.phar require "maximal/yii2-webp-thumbnailer" "*"

或者

"maximal/yii2-webp-thumbnailer": "*"

将其添加到您的应用程序的composer.json文件中的require部分。

检查环境

您需要在系统中安装WebP编码器(cwebp命令)。

例如,在Ubuntu/Debian中,它包含在webp包中

sudo apt install webp

检查命令

cwebp -version

您应该看到一个带有版本号(如0.6.1)的输出。

如果您已将cwebp安装到不同的命令或路径,在使用辅助工具之前配置静态属性WebpThumbnailer::$cwebpCommand(请参阅下面的示例)。

有关WebP的更多信息:https://developers.google.com/speed/webp/

生成缩略图

在您的Yii2应用程序中使用此缩略图生成器

use maximal\thumbnail\yii\WebpThumbnailer;

echo WebpThumbnailer::picture('@webroot/img/image.png', $width, $height);

更多选项(使用outbound代替默认的inset;添加altclass属性)

use maximal\thumbnail\yii\WebpThumbnailer;

echo WebpThumbnailer::picture(
	'@webroot/img/image.png',
	$width,
	$height,
	false,
	['alt' => 'Alt attribute', 'class' => 'img-responsive']
);

自定义cwebp命令

use maximal\thumbnail\yii\WebpThumbnailer;

WebpThumbnailer::$cwebpCommand = '/usr/local/bin/cwebp';
echo WebpThumbnailer::picture('@webroot/img/image.jpg', $width, $height);

辅助工具的picture()方法使用以下现代<picture>HTML标签

<picture data-cache="hit|new">
	<source srcset="/assets/thumbnails/...image.png.webp" type="image/webp" />
	<img src="/assets/thumbnails/...image.png" other-attributes="" />
</picture>

这里为支持WebP图像的浏览器提供了image/webp源,并为传统图像(PNG、JPEG、TIFF、GIF)提供了回退。

作者