Home  >  Article  >  Backend Development  >  How to convert jpg in php webp

How to convert jpg in php webp

藏色散人
藏色散人Original
2020-08-25 10:32:523424browse

php Method for converting webp to jpg: 1. Use the "webptojpgapi" method to convert webp to jpg format; 2. Call the system library to convert webp to jpg, with code such as "@system("dwebp $img_pathwebp -o $pic_path"".

How to convert jpg in php webp

Recommended: "PHP Video Tutorial"

php webp to jpg

I recently worked on a project that involved cutout,

but the webp format cannot be displayed on Firefox

Solution:

//webp转换成jpg格式
function webptojpgapi($inputurl,$inputname){
    $apiurl = "https://api.cloudconvert.com/convert?apikey=[自己的apikey]&input=download&filename=$inputname&download=false&save=true&inputformat=webp&outputformat=jpg&file=$inputurl";
    $res = file_get_contents($apiurl);
    $res = json_decode($res,true);
    return $res['output']['url'].'/'.str_replace('webp','jpg',$inputname);
}

All available on the official website Explanation, this is the specific,

But this has the inconvenience, charging, damn, cheating, so I adopted another method.

Call the system library, the code is as follows, He looks very classy

if($ext=='webp'){
      $img_pathwebp = "Runtime/yy_".time().rand_string(3).'.png';
@file_put_contents($img_pathwebp, file_get_contents($img_path));
      $img_pathwebp = realpath($img_pathwebp);
$pic_path  = $img_pathwebp ; 
      
      @system("dwebp $img_pathwebp -o $pic_path", $retval);
 }

The above is the detailed content of How to convert jpg in php webp. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn