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);
?>
输出:
范例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);
?>
输出: