Home >Backend Development >PHP Problem >Introduction to the method of converting php webp to jpg

Introduction to the method of converting php webp to jpg

藏色散人
藏色散人Original
2020-10-15 10:30:283603browse

php webp to jpg method: first open the corresponding code file; then call the system library; finally convert webp to jpg through "if($ext=='webp'){...}" method Can.

Introduction to the method of converting php webp to jpg

Recommended: "PHP Video Tutorial"

Related introduction: WebP (pronounced: weppy) is An image file format that provides both lossy compression and lossless compression (reversible compression). It is derived from the image encoding format VP8 and is considered a sister project of the WebM multimedia format. It was developed by Google after purchasing On2 Technologies and is based on BSD Release of licensing terms.

JPEG (Joint Photographic Experts Group) is the product of the JPEG standard, which was developed by the International Organization for Standardization (ISO) and is a compression standard for continuous-tone still images. JPEG format is the most commonly used image file format, with the suffix .jpg or .jpeg.

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);
}

There are instructions on the official website, this is the specifics,

But there is an inconvenience, the fee, oh, it’s a rip-off, so Another approach was adopted.

Call the system library, the code is as follows, it 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 Introduction to the method of converting php webp to jpg. 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