imagecolortransparent
将颜色定义为透明
此函数自 PHP 4.3.0 版本开始可用。
imagecolortransparent 函数用于设置图像中的透明颜色。此函数可以让一个特定的颜色成为透明颜色,通常用于 GIF 图像格式中。
int imagecolortransparent(resource $image, int $color)
返回设置为透明颜色的颜色索引。如果成功,返回透明颜色的索引值;如果失败,返回 -1。
以下示例演示如何将一个特定的颜色设为透明色。
在此示例中,首先我们创建了一个 100x100 像素的图像,并分配了一些颜色。然后使用 imagecolortransparent 函数将创建的绿色颜色设为透明色。
<?php
// 创建一个 100x100 的图像
$image = imagecreatetruecolor(100, 100);
// 分配颜色
$white = imagecolorallocate($image, 255, 255, 255);
$green = imagecolorallocate($image, 0, 255, 0);
// 用白色填充图像
imagefill($image, 0, 0, $white);
// 设置绿色为透明色
imagecolortransparent($image, $green);
// 输出图像
header('Content-Type: image/png');
imagepng($image);
// 释放内存
imagedestroy($image);
?>
在这个例子中,绿色部分将变成透明,适用于如 GIF 图像类型中透明色的应用。