maximal / yii2-avif-thumbnailer
基于Imagine的Yii2助手,用于实时创建和缓存AVIF缩略图
v1.0.0
2022-11-24 13:18 UTC
Requires
- php: ^8.0|^8.1|^8.2
- maximal/php-avif-thumbnailer: ^1.0
- yiisoft/yii2-imagine: ^2.3
README
这是一个基于PHP AVIF缩略图生成器的Yii2助手,允许您在Yii2应用程序中动态生成和缓存图像AVIF缩略图。
安装
使用Composer安装此库
composer require "maximal/yii2-avif-thumbnailer"
或将以下内容
"maximal/yii2-avif-thumbnailer": "^1.0"
添加到应用程序的composer.json
文件中的require
部分。
检查环境
您需要在系统中安装带有AVIF(AV1)编解码器的FFMPEG。
例如,在Ubuntu/Debian中,它包含在ffmpeg
软件包中
sudo apt install ffmpeg
检查命令
ffmpeg -version
您应该得到一个包含版本号的输出。版本号在4.3
之后应该都是可以的。
如果您将ffmpeg
安装到了不同的命令或路径,请在使用助手之前配置静态属性AvifThumbnailer::$ffmpegCommand
(请参见下面的示例)。
有关AVIF的更多信息:https://avif.io/blog/
生成缩略图
在您的Yii2应用程序中使用此缩略图生成器
use Maximal\Thumbnailers\Yii2\AvifThumbnailer; echo AvifThumbnailer::picture('@webroot/img/image.png', $width, $height);
更多选项(使用outbound
而不是默认的inset
;添加了alt
和class
属性)
use Maximal\Thumbnailers\Yii2\AvifThumbnailer; echo AvifThumbnailer::picture( '@webroot/img/image.png', $width, $height, false, ['alt' => 'Alt attribute', 'class' => 'img-responsive'] );
自定义ffmpeg
命令
use Maximal\Thumbnailers\Yii2\AvifThumbnailer; AvifThumbnailer::$ffmpegCommand = '/usr/local/bin/ffmpeg'; echo AvifThumbnailer::picture('/path/to/img/image.jpg', $width, $height);
助手的方法picture()
使用以下现代的<picture>
HTML标签
<picture data-cache="hit|new|fail"> <source srcset="/assets/thumbnails/...image.png.avif" type="image/avif" /> <img src="/assets/thumbnails/...image.png" other-attributes="" /> </picture>
这里为支持AVIF图像的浏览器提供image/avif
源,并为传统(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