maximal / yii2-webp-thumbnailer
基于Imagine的Yii2辅助工具,用于实时创建和缓存WebP缩略图
v1.0.0
2020-05-07 13:22 UTC
Requires
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
;添加alt
和class
属性)
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)提供了回退。
作者
- 网站:https://maximals.ru 和 https://sijeko.ru
- StackOverflow故事:http://stackoverflow.com/users/story/1021887
- Twitter:https://twitter.com/almaximal
- Telegram:https://t.me/maximal