Because the project requirements need to convert JPG, PNG type pictures to WEBP format, the first use of the PHP GD class library IMAGEWEBP method implementation, the results found that the converted Webp format files will occasionally appear blank content. Like creating a picture of a transparent layer, but the size and memory are indistinguishable to the successful conversion of the file.
At first thought, can find a way to determine whether a piece is invalid, find all kinds of methods on the net, the result is fruitless. Although failed, but still the code, expect the great God to help improve the method.
functionIMGTOWEBP ($file= ' '){ if(Is_file($file)){ $img=getimagesize($file); $type=$img[' MIME ']; if($type= = ' Image/jpeg '){ $file _source= Imagecreatefromjpeg ($file); } ElseIf($type= = ' Image/png '){ $file _source= Imagecreatefrompng ($file); } Else{ return false; } $i=Strripos($file,‘.‘); $newFileName=substr($file, 0,$i+1). ' Webp; $ret= IMAGEWEBP ($file _source,$newFileName, ' 80 '); return $ret; }}
Use the prerequisite, PHP version >=5.5 above, and open the GD class library.
The success rate of the above method is not very high, welcome to the wrong point. Because the success rate is not high, I changed the realization way decisively.
Since PHP couldn't help me with my task, I naturally wanted the third-party class library on Linux to help me with the format conversion, so I found the LIBWEBP library.
1. First install type support
# ubuntuapt-#centosyum-y Install Libwebp-devel libwebp-tools
After installation, you will get three tools in your system (VWEBP, Webpmux to learn)
1. CWEBP If you need help cwebp--help, paste a sample conversion command:
CWEBP-Q ~/***.jpg-o ***.webp
2. Dwebp convert WEBP files to JGP, PNG, ditto help: Dwebp--help
DWEBP ~/***.webp-o ~/***.png
3. GIF2WEBP GIF image resources into WEBP
CWEBP-Q ~/***.gif-o ***.webp
With the system's class library support, the program directly throw command to Linux processing can be, I use PHP, I remember that if the online mode your PHP environment may have the exec, system and other functions disabled, view your php.ini,disable_funcitons configuration items. Warning: Disabling function opening will increase the security risk of the server, so please check the parameters in the program to ensure that your post-filtering commands do not cause unnecessary loss.
Welcome Daniel at any time to criticize and correct!!!
Also need to make sure that your server has libpng, libjpeg and other support packages installed.
Convert (JGP, PNG) images to WEBP format under CentOS