How to create a .webp image in PHP

依然范特西╮ 提交于 2019-12-01 15:17:14

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.

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.

webp images creating process:

you can use following php commands,to get the webp images

$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

http://php.net/manual/en/function.exec.php

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:

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:

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!

There are now several npm packages to create .webp images from PNG, JPEG and TIFF formats.

Here's one Gulp plugin as an example - gulp-webp.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!