如何在 PHP 中创建 .webp 图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25248382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a .webp image in PHP
提问by Brett DeWoody
How do you create .webp
images using PHP?
你如何.webp
使用 PHP创建图像?
Modern versions of PHP (>= 5.5.0) can be compiled with WebP
support but from I've seen this isn't common on many web hosts. If compiled with WebP support you can use the built-in imagewebp()
functionto create .webp
images.
现代版本的 PHP (>= 5.5.0) 可以在WebP
支持下编译,但据我所知,这在许多网络主机上并不常见。如果使用 WebP 支持编译,您可以使用内置imagewebp()
函数来创建.webp
图像。
What are the alternatives for creating .webp
images using PHP? Libraries, APIs other methods?
.webp
使用 PHP创建图像的替代方法是什么?库、APIs 其他方法?
采纳答案by rosell.dk
The options currently available are: gd (extension), imagick (extension), imagick (exec-call), gmagick (extension), gmagick (exec-call), cwebp (exec-call), gmagick (exec call) or calling a cloud service. I have created a library 'webp-convert' on github which tries all methods. The readme-file describes the pros and cons of each method. Its available here: https://github.com/rosell-dk/webp-convert.
当前可用的选项有:gd(扩展)、imagick(扩展)、imagick(exec-call)、gmagick(扩展)、gmagick(exec-call)、cwebp(exec-call)、gmagick(exec 调用)或调用一个云服务。我在 github 上创建了一个库“webp-convert”,它尝试了所有方法。自述文件描述了每种方法的优缺点。它可以在这里找到:https: //github.com/rosell-dk/webp-convert。
For reasons unknown to me, the imagick/gmagick extensions produces no better quality than the original files. This is only a problem with the extensions, not the exec calls.
由于我不知道的原因,imagick/gmagick 扩展产生的质量并不比原始文件好。这只是扩展的问题,而不是 exec 调用的问题。
回答by Terry
You can go right to Google and build the WebP libraries from source. Use this link to get the appropriate archive for your operating system:
您可以直接访问 Google 并从源代码构建 WebP 库。使用此链接为您的操作系统获取适当的存档:
https://developers.google.com/speed/webp/docs/compiling#building
https://developers.google.com/speed/webp/docs/compiling#building
Then you can use the following command within a php system() function to convert the images:
然后您可以在 php system() 函数中使用以下命令来转换图像:
Syntax:
句法:
cwebp [quality
qualitypercentage] [source
image] -o [destination]`
cwebp -q 80 source.png -o
destination.webp
I would recommend reading the above link to get your libraries compiled, then go here to get more information about using the libraries.
我建议阅读上面的链接来编译你的库,然后去这里获取有关使用库的更多信息。
Best of luck with the project!
祝项目好运!
回答by Elangovan
webp images creating process:
webp图片制作过程:
you can use following php commands,to get the webp images
您可以使用以下 php 命令来获取 webp 图像
$imgName = "codingslover.jpg";
$webPName = "codingslover.webp";
Syntax:
cwebp [quality qualitypercentage] [source image] -o [destination]
exec("cwebp -q 0 ".$imgName." -o ".$webPName." ");
Anthor Method:
exec("convert -colorspace RGB ".$imgName." ".$webPName . " ");
Exec: executes the given command in php
Exec: 在 php 中执行给定的命令
回答by Kiran Maniya
You can use Intervention Image Library. It provides various image encoding options and utilities related to image formatting. Hereis the sample code snippet to convert image into webp
format. It supports numerous image encodings as listed below,
您可以使用干预图像库。它提供了与图像格式相关的各种图像编码选项和实用程序。这是将图像转换为webp
格式的示例代码片段。它支持多种图像编码,如下所列,
- jpg — return JPEG encoded image data
- png — return Portable Network Graphics (PNG) encoded image data
- gif — return Graphics Interchange Format (GIF) encoded image data
- tif — return Tagged Image File Format(TIFF) encoded image data
- bmp — return Bitmap (BMP) encoded image data
- ico — return ICO encoded image data
- psd — return Photoshop Document (PSD) encoded image data
- webp — return WebP encoded image
- data data-url — encode current image data in data URI scheme (RFC 2397)
- jpg — 返回 JPEG 编码的图像数据
- png — 返回便携式网络图形 (PNG) 编码的图像数据
- gif — 返回图形交换格式 (GIF) 编码的图像数据
- tif — 返回标记图像文件格式 (TIFF) 编码的图像数据
- bmp — 返回位图 (BMP) 编码的图像数据
- ico — 返回 ICO 编码的图像数据
- psd — 返回 Photoshop 文档 (PSD) 编码的图像数据
- webp — 返回 WebP 编码的图像
- data data-url — 在数据 URI 方案 (RFC 2397) 中编码当前图像数据