maximal/php-webp-thumbnailer

PHP (基于 Imagine) 助手,用于实时创建和缓存 WebP 缩略图

v1.0.0 2021-12-16 10:08 UTC

This package is auto-updated.

Last update: 2024-09-18 15:32:46 UTC


README

WebpThumbnailer 是一个缩略图助手,允许您在 PHP 应用程序中实时生成和缓存图像缩略图。

安装

使用 Composer 安装此库

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

或者

"maximal/php-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/

生成缩略图

在您的 PHP 应用程序中使用此缩略图器

use maximal\thumbnail\WebpThumbnailer;

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

更多选项(使用 outbound 而不是默认的 inset;添加 altclass 属性)

use maximal\thumbnail\WebpThumbnailer;

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

自定义 cwebp 命令

use maximal\thumbnail\WebpThumbnailer;

WebpThumbnailer::$cwebpCommand = '/usr/local/bin/cwebp';
echo WebpThumbnailer::picture('/path/to/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)图像回退。

作者