php 生成 webp,PHP imagewebp()用法及代码示例

本文介绍了PHP内置函数imagewebp(),用于将图像转换为WebP格式并在浏览器中显示。文中提供了三个示例,分别演示了在浏览器中显示WebP图像、将PNG转换为WebP以及调整WebP图像质量的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

imagewebp()函数是PHP中的内置函数,用于向浏览器或文件显示图像。此功能的主要用途是在浏览器中查看图像,将任何其他图像类型转换为WebP并更改图像质量。

bool imagewebp( resource $image, int $to, int $quality)

参数:此函数接受上述和以下所述的三个参数:

$image:它指定要处理的图像资源。

$to (Optional):它指定将文件保存到的路径。

$quality (Optional):它指定图像的质量。

返回值:如果成功,此函数将返回TRUE,否则将返回FALSE。

以下示例说明了PHP中的imagewebp()函数:示例1:在此示例中,我们将在浏览器中查看图像。

的PHP

// Load an image from local webp file

// Images can be converted into webp

// using imagewebp() function or other

// online convertors

$im = imagecreatefromwebp('./geeksforgeeks.webp');

// View the loaded image in browser

// using imagewebp() function

header('Content-type:image/webp');

imagewebp($im);

imagedestroy($im);

?>

输出:

228c98d6ff62e907625fff7fe242f4b7.png

范例2:在此示例中,我们将PNG转换为WEBP。

的PHP

// Load an image from local webp file

// Images can be converted into webp

// using imagewebp() function or other

// online convertors

$im = imagecreatefromwebp('./geeksforgeeks.webp');

// Convert the image into webp

// using imagewebp() function

imagewebp($im, 'converted.webp');

imagedestroy($im);

?>

输出:

This will save the WEBP version of image in the same folder where your PHP script is.

程序3:在此示例中,我们将更改图像质量。

的PHP

// Load an image from local webp file

// Images can be converted into webp

// using imagewebp() function or other

// online convertors

$im = imagecreatefromwebp('./geeksforgeeks.webp');

// Convert the image into webp using imagewebp()

// function and view in the browser

header('Content-type:image/webp');

imagewebp($im, null, 0);

imagedestroy($im);

?>

输出:

03f44d588e94c2166f324cc53ab0b85a.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值