我的骄傲

我的骄傲

个人的网络空间

php任意图片转成webp格式

本文发布于:2024-04-19

php任意图片转成webp格式

 

function webpImage($source, $quality = 100, $removeOld = false)
    {
        $dir = pathinfo($source, PATHINFO_DIRNAME);
        $name = pathinfo($source, PATHINFO_FILENAME);
        $destination = $dir . DIRECTORY_SEPARATOR . $name . '.webp';
        $info = getimagesize($source);
        $isAlpha = false;
        if ($info['mime'] == 'image/jpeg')
            $image = imagecreatefromjpeg($source);
        elseif ($isAlpha = $info['mime'] == 'image/gif') {
            $image = imagecreatefromgif($source);
        } elseif ($isAlpha = $info['mime'] == 'image/png') {
            $image = imagecreatefrompng($source);
        } else {
            return $source;
        }
        if ($isAlpha) {
            imagepalettetotruecolor($image);
            imagealphablending($image, true);
            imagesavealpha($image, true);
        }
        imagewebp($image, $destination, $quality);
        if ($removeOld)  unlink($source);
        return $destination;
    }

 

我的骄傲

我的骄傲

个人的网络空间

联络

  • EMail: shadoweb@qq.com
  • QQ: 925474725
  • 地址: 深圳

标签

链接