将图片webp转成png格式进行展示

将图片webp转成png格式进行展示

代码片.

/**
 * @param $webpPath webp路径
 * @param $outputPath 输出路径
 * @return false|void
 */
function convertWebPToPNG($webpPath, $outputPath) {
    // 打开 WebP 图片
    $image = @imagecreatefromwebp($webpPath);
    if ($image === false) {
        // 处理无法打开 WebP 图片的情况
        return false;
    }
    // 创建 PNG 图片
    $pngImage = imagecreatetruecolor(imagesx($image), imagesy($image));
    // 将 WebP 图片转换为 RGBA 格式
    imagealphablending($pngImage, false);
    imagesavealpha($pngImage, true);
    $transparent = imagecolorallocatealpha($pngImage, 0, 0, 0, 127);
    imagefill($pngImage, 0, 0, $transparent);
    // 将 WebP 图片复制到 PNG 图片中
    imagecopy($pngImage, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
    // 保存 PNG 图片到指定路径
    imagepng($pngImage, $outputPath);
    // 释放内存
    imagedestroy($image);
    imagedestroy($pngImage);
}
// 使用示例
$webpPath = 'D:\download\1052800645_0_0_3072_2048_1440x900_80_0_1_410a14bb85b58b9f694c6088aaa28315.jpg.webp';
$outputPath = 'D:\download\1052800645_0_0_3072_2048_1440x900_80_0_1_410a14bb85b58b9f694c6088aaa28315.png';
convertWebPToPNG($webpPath, $outputPath);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值