牛牛在线工具

imagewebp()

收录 8868 个 PHP 函数,含用途说明与用法示例。

imagewebp()

将图像以 WebP 格式输出或保存到文件中

函数名称:imagewebp() 适用版本:PHP 5.4.0 及以上版本 函数描述:imagewebp() 函数用于将图像以 WebP 格式输出或保存到文件中。 语法:bool imagewebp ( resource $image , string $filename [, int $quality = 80 ] ) 参数: $image:图像资源,由 imagecreatefromjpeg()、imagecreatefrompng() 等函数创建。 $filename:保存图像的文件路径。 $quality(可选):图像质量,取值范围为 0 到 100,默认为 80。 返回值:成功时返回 true,失败时返回 false。 示例: // 创建一个空白图像 $image = imagecreatetruecolor(200, 200); // 设置图像背景颜色为白色 $bgColor = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $bgColor); // 画一个红色的矩形 $rectColor = imagecolorallocate($image, 255, 0, 0); imagefilledrectangle($image, 50, 50, 150, 150, $rectColor); // 输出图像为 WebP 格式 imagewebp($image, 'output.webp', 90); // 释放图像资源 imagedestroy($image); 以上示例代码创建了一个200x200像素的空白图像,设置背景颜色为白色,并在图像中间画了一个红色的矩形。最后使用 imagewebp() 函数将图像保存为名为 "output.webp" 的 WebP 文件,图像质量为 90。