maximal / php-avif-thumbnailer
PHP (通过 Imagine) 实时创建和缓存 AVIF 缩略图的辅助工具
v1.0.0
2022-11-24 13:05 UTC
Requires
- php: ^8.0|^8.1|^8.2
- imagine/imagine: ^1.3
README
AvifThumbnailer
是一个缩略图辅助工具,允许您在 PHP 应用程序中动态生成和缓存图像缩略图。
安装
使用 Composer 安装此库
composer require "maximal/php-avif-thumbnailer"
或在您的应用程序的 composer.json
文件的 require
部分中添加:
"maximal/php-avif-thumbnailer": "^1.0"
检查环境
您需要在系统中安装支持 AVIF (AV1) 编码器的 FFMPEG。
例如,在 Ubuntu/Debian 中,它包含在 ffmpeg
软件包中
sudo apt install ffmpeg
检查命令
ffmpeg -version
您应该得到一个包含版本号的输出。任何高于 4.3
的版本都应该没问题。
如果您已将 ffmpeg
安装到不同的命令或路径,请在使用辅助工具之前配置静态属性 AvifThumbnailer::$ffmpegCommand
(见以下示例)。
有关 AVIF 的更多信息: https://avif.io/blog/
生成缩略图
在您的 PHP 应用程序中使用此缩略图生成器
use Maximal\Thumbnailers\AvifThumbnailer; echo AvifThumbnailer::picture('/path/to/img/image.png', $width, $height);
更多选项(使用 outbound
而不是默认的 inset
;添加 alt
和 class
属性)
use Maximal\Thumbnailers\AvifThumbnailer; echo AvifThumbnailer::picture( '/path/to/img/image.png', $width, $height, false, ['alt' => 'Alt attribute', 'class' => 'img-responsive'] );
自定义 ffmpeg
命令
use Maximal\Thumbnailers\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
- 推特: https://twitter.com/almaximal
- 电报: https://t.me/maximal