rosell-dk / webp-convert
使用 PHP 将 JPEG 和 PNG 转换为 WebP
Requires
- php: ^5.6 | ^7.0 | ^8.0
- rosell-dk/exec-with-fallback: ^1.2.0
- rosell-dk/image-mime-type-guesser: ^1.1.1
- rosell-dk/locate-binaries: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.11
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.3
- squizlabs/php_codesniffer: 3.*
Suggests
- ext-gd: to use GD extension for converting. Note: Gd must be compiled with webp support
- ext-imagick: to use Imagick extension for converting. Note: Gd must be compiled with webp support
- ext-vips: to use Vips extension for converting.
- dev-master
- 2.9.3
- 2.9.2
- 2.9.1
- 2.9.0
- 2.8.0
- 2.7.0
- 2.6.0
- 2.5.0
- 2.4.0
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 2.0.0-beta
- 1.3.9
- 1.3.8
- 1.3.7
- 1.3.6
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.1
- 1.1.0
- 1.1.0-beta
- 1.0.0
- 1.0.0-beta
- 0.1.0
This package is auto-updated.
Last update: 2024-09-07 05:56:21 UTC
README
使用 PHP 将 JPEG 和 PNG 转换为 WebP
此库使您能够使用 PHP 进行 webp 转换。它支持大量转换方法,并自动选择系统上可用的最强大的方法。
库可以使用以下方法进行转换
- cwebp (通过
exec
调用执行 cwebp 二进制文件) - vips (使用 Vips PHP 扩展)
- imagick (使用 Imagick PHP 扩展)
- gmagick (使用 Gmagick PHP 扩展)
- imagemagick (通过
exec
调用执行 imagemagick 二进制文件) - graphicsmagick (通过
exec
调用执行 graphicsmagick 二进制文件) - ffmpeg (通过
exec
调用执行 ffmpeg 二进制文件) - wpc (使用基于此库的 WebPConvert Cloud Service - PHP 的开源 webp 转换器)
- ewww (使用 ewww 云转换器(1 美元启动费,然后免费 webp 转换))
- gd (使用 Gd PHP 扩展)
除了转换外,库还有一个用于 服务 转换图像的方法,我们在此处提供了如何设置自动将 webp 图像发送到支持 webp 的浏览器的解决方案的说明。
安装
使用 Composer 需要库,如下所示
composer require rosell-dk/webp-convert
转换图像
以下是使用 WebPConvert::convert 方法进行转换的最小示例
// Initialise your autoloader (this example is using Composer) require 'vendor/autoload.php'; use WebPConvert\WebPConvert; $source = __DIR__ . '/logo.jpg'; $destination = $source . '.webp'; $options = []; WebPConvert::convert($source, $destination, $options);
WebPConvert::convert 方法附带一些选项。以下介绍是必读的:[docs/v2.0/converting/introduction-for-converting.md](https://github.com/rosell-dk/webp-convert/blob/master/docs/v2.0/converting/introduction-for-converting.md)。
如果您是从 1.3.9 迁移,请[阅读此内容](https://github.com/rosell-dk/webp-convert/blob/master/docs/v2.0/migrating-to-2.0.md)
服务转换图像
WebPConvert::serveConverted 方法尝试提供服务转换的图像。如果目标位置已存在图像,则将使用该图像,除非原始图像较新或较小。如果方法无法提供转换图像,则将提供服务原始图像、404 或 'fail' 选项设置的任何内容。它还添加了 X-WebP-Convert-Log 头部,该头部提供了有关发生情况的见解。
示例(版本 2.0)
require 'vendor/autoload.php'; use WebPConvert\WebPConvert; $source = __DIR__ . '/logo.jpg'; $destination = $source . '.webp'; WebPConvert::serveConverted($source, $destination, [ 'fail' => 'original', // If failure, serve the original image (source). Other options include 'throw', '404' and 'report' //'show-report' => true, // Generates a report instead of serving an image 'serve-image' => [ 'headers' => [ 'cache-control' => true, 'vary-accept' => true, // other headers can be toggled... ], 'cache-control-header' => 'max-age=2', ], 'convert' => [ // all convert option can be entered here (ie "quality") ], ]);
以下介绍是必读的(针对 2.0):[docs/v2.0/serving/introduction-for-serving.md](https://github.com/rosell-dk/webp-convert/blob/master/docs/v2.0/serving/introduction-for-serving.md)。
旧介绍(针对 1.3.9)在此处提供:[docs/v1.3/serving/convert-and-serve.md](https://github.com/rosell-dk/webp-convert/blob/master/docs/v1.3/serving/convert-and-serve.md)
按需 WebP
该库可用于创建一个按需WebP解决方案,它会自动为支持WebP的浏览器提供WebP图像,而不是jpeg/pngs。要设置此功能,请遵循以下教程中的说明(尚未更新为2.0版本):这里。
使用WebP Convert的项目
使用WebP Convert的CMS插件
该库作为引擎为一些平台提供webp转换。希望这个列表会随着时间的推移而增长。目前,以下CMS的插件/扩展/模块(按市场份额排序)使用了该库:
其他使用WebP Convert的项目
-
webp-convert-cloud-service 基于WebPConvert的云服务
-
webp-convert-concat 将webp-convert库及其依赖项合并为单个PHP文件(或两个)
支持WebP Convert
桌上的面包并非免费,即使这个库是,而且永远都是。我喜欢开发这个,也喜欢支持你们,但我也需要面包。请让我两者兼得。
支持者
目前通过Patreon支持项目的人员 - 感谢!
- Max Kreminsky
- Nodeflame
- Mathieu Gollain-Dupont
- Ruben Solvang
最近通过ko-fi贡献的人员 - 感谢!
- 18 Oct: Magestyx
- 10 Oct: Jesper
- 4 Oct: Caio Nogueira
- 22 Sep: Mark
- 8 Sep: Brinsley
- 4 Aug: Henri
- 13 Jun: Pat
- 2 May: Label Vier
- 28 Apr: Nealboy
- 11 Apr: 匿名
- 3 Apr: Jakub
- 28 Mar: ciriman
- 31 Jan: Aron
贡献额外慷慨金额的咖啡/终身支持(>50$)的人员 - 感谢!
- Max Kreminsky ($115)
- Justin - BigScoots ($105)
- Bill Vallance ($102)
- Label Vier ($100)
- Sebastian ($99)
- Tammy Lee ($90)
- Steven Sullivan ($51)
- Mathieu Gollain-Dupont ($50)
- Erica Dreisbach ($50)
- Brian Laursen ($50)
- Dimitris Vayenas ($50)
2.9.0版本中的新增功能(发布于2021年12月7日,在我女儿10岁生日那天!)
- 当exec()不可用时,现在会尝试其他替代方案(如通过proc_open()、passthru()等模拟)。使用此库来完成此操作。
- 当缺少将调色板图像转换为RGB所需的功能时,Gd现在会被标记为不可操作。理由:半工作的转换器比标记为不可操作的问题更大。
- 改进了CI测试。现在它在Windows、Mac上进行了测试,并且在不启用功能的情况下(如exec()被禁用时)进行了测试。
- 还有更多(在此查看已关闭的问题这里)
2.8.0版本中的新增功能
- 转换器选项定义现在可以访问,包括建议的UI和帮助文本。这允许根据转换选项自动生成前端。该功能已在WebP Express中使用的webp-convert文件管理器中使用。新方法:
WebPConvert::getConverterOptionDefinitions()
- 显示选项的部分日志现在更容易阅读。它现在还会警告关于已弃用的选项。
- 将图像 MIME 类型猜测库升级到 0.4。这个版本可以通过嗅探前几个字节来检测更多 MIME 类型。
- 还有更多(查看已关闭的问题 这里)
2.7.0 中的新功能
- ImageMagick 现在支持 "近似无损" 选项(需要 Imagick >= 7.0.10-54)#299
- 为 ImageMagick 添加了 "try-common-system-paths" 选项(默认:true)。因此,ImageMagick 现在将在常用系统路径中查找 "convert" #293
- 修复了在非常旧的 PHP 版本中 Gd 的内存泄漏问题 #264
- 还有更多(查看已关闭的问题 这里)
2.6.0 中的新功能
- 引入了 auto-limit 选项,用于替代将 "quality" 设置为 "auto" #281
- 添加了 "sharp-yuv" 选项,并使其成为默认选项。非常好,使用它!在大多数转换器中都能工作(在 cwebp、vips、imagemagick、graphicsmagick、imagick 和 gmagick 中工作)#267,#280,#284
- 将 cwebp 二进制文件升级到 1.2.0 #273
- vips 现在支持 "method" 选项和 "preset" 选项。
- graphicsmagick 现在支持 "auto-filter" 选项
- vips、imagick、imagemagick、graphicsmagick 和 gmagick 现在支持 "preset" 选项 #275
- cwebp 现在仅在必要时验证所提供预编译二进制的哈希。这减少了转换时间。 #287
- 为 cwebp 添加了 新选项,以跳过在当前系统上已知无法工作的预编译二进制文件。这将减少转换时间。 #288
- 还有更多(查看已关闭的问题 这里)