牛牛在线工具

imagegd()

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

imagegd()

将图像以 GD 1.x 格式保存到指定的文件或浏览器

函数名:imagegd() 适用版本:PHP 4、PHP 5、PHP 7 函数描述: imagegd() 函数将图像以 GD 1.x 格式保存到指定的文件或浏览器。 语法: bool imagegd ( resource $image [, mixed $to [, int $threshold = -1 ]] ) 参数: image:图像资源,由 imagecreatetruecolor() 或 imagecreate() 创建。 to:可选参数,指定保存的文件路径。如果未指定,则图像将直接输出到浏览器。 threshold:可选参数,指定图像色彩阈值,范围为 0 到 255。默认值为 -1,表示未设置阈值。 返回值: 成功时返回 true,失败时返回 false。 示例: 将图像保存到文件: $image = imagecreatetruecolor(200, 200); $white = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $white); $filename = 'output.gd'; if (imagegd($image, $filename)) { echo '图像保存成功!'; } else { echo '图像保存失败!'; } 直接输出图像到浏览器: $image = imagecreatetruecolor(200, 200); $white = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $white); header('Content-Type: image/png'); imagegd($image); 注意事项: 在使用 imagegd() 函数之前,需要先创建一个图像资源。 如果指定了保存的文件路径,需要确保 PHP 有写入权限。 如果未指定保存的文件路径,则需要在调用 imagegd() 之前设置正确的 HTTP 头信息,以便将图像正确输出到浏览器。