使用WebP

使用WebP

WebP旨在缩减图片尺寸,并且效果十分可观,尤其是面对png格式的图片时,表现更是让人伸向深刻。

#——原图
fm_1.png	573kb
#——转换成webp后
fm_1.webp	 36kb

Node生成

$ npm install cwebp
$ cwebp -q 75 -m 4 fm_1.png -o fm_1.webp

参数说明:

  • -q 图片质量,75为谷歌/腾讯推荐
  • -m 图片压缩比,4为谷歌推荐
  • -o 生成

PHP生成

PHP官方提供了imagecreatewebp函数来进行jpg/jpeg/png转换为webp格式的图片,pngwebp依然效果突出。

<?php

$oldImg = 'fm_1.png';
$newImg = 'new_fm_1.webp';

//注意当图片格式不同时,函数imagecreatefrom后接png/jpeg, jpeg又包括了jpg和jpeg两种格式
$img = imagecreatefrompng($oldImg);
imagepalettetotruecolor($img);
imagealphablending($img,true);
imagesavealpha($img,true);
imagewebp($img,$newImg,75);
imagedestroy($img);

参考文档

  1. imagecreatefrompng函数
posted @ 2022-01-12 19:37  Marhey  阅读(173)  评论(0)    收藏  举报