imagecolortransparent() 函数是 PHP 的 GD 库中的一个函数,用于设置 PNG 或 GIF 图像的透明色
以下是 imagecolortransparent() 函数在不同图片格式中的应用示例:
// 创建一个宽度为 200、高度为 200 的真彩色图像$image = imagecreatetruecolor(200, 200);// 为图像分配颜色$white = imagecolorallocate($image, 255, 255, 255);$red = imagecolorallocate($image, 255, 0, 0);// 使用白色填充背景imagefill($image, 0, 0, $white);// 绘制一个红色矩形imagerectangle($image, 50, 50, 150, 150, $red);// 将白色设置为透明色imagecolortransparent($image, $white);// 输出 PNG 图像header("Content-type: image/png");imagepng($image);imagedestroy($image);GIF 图像:// 创建一个宽度为 200、高度为 200 的调色板图像$image = imagecreate(200, 200);// 为图像分配颜色$white = imagecolorallocate($image, 255, 255, 255);$red = imagecolorallocate($image, 255, 0, 0);// 使用白色填充背景imagefill($image, 0, 0, $white);// 绘制一个红色矩形imagerectangle($image, 50, 50, 150, 150, $red);// 将白色设置为透明色imagecolortransparent($image, $white);// 输出 GIF 图像header("Content-type: image/gif");imagegif($image);imagedestroy($image);在这两个示例中,我们首先创建了一个宽度为 200、高度为 200 的图像(一个真彩色图像和一个调色板图像)。然后,我们为图像分配了白色和红色。接下来,我们使用白色填充背景,并绘制了一个红色矩形。最后,我们将白色设置为透明色,并输出 PNG 或 GIF 图像。


