PHP 中的 imagecolorallocatealpha() 函数
phpserver side programmingprogramming更新于 2025/7/6 8:22:17
imagecolorallocatealpha() 函数用于为图像分配颜色。
语法
imagecolorallocatealpha ( img, red, green, blue, alpha )
参数
img:使用 imagecreatetruecolor() 创建的图像资源。
red:红色成分
green:绿色成分
blue:蓝色成分
alpha: 图像的透明度,0 表示完全不透明,127表示完全透明。
返回
imagecolorallocatealpha() 函数返回颜色标识符,如果分配失败则返回 FALSE。
示例
以下是示例:
<?php
$img = imagecreatetruecolor(520, 350);
$bgcolor = imagecolorallocate($img, 50, 10, 255);
imagefill($img, 0, 0, $bgcolor);
$one = imagecolorallocatealpha($img, 50, 255, 0, 70);
$two = imagecolorallocatealpha($img, 255, 0, 255, 50);
$three = imagecolorallocatealpha($img, 150, 255, 0, 60);
$four = imagecolorallocatealpha($img, 200, 0, 255, 90);
imagefilledellipse($img, 200, 150, 150, 150, $one);
imagefilledellipse($img, 220, 150, 150, 150, $two);
imagefilledellipse($img, 240, 150, 150, 150, $three);
imagefilledellipse($img, 280, 150, 150, 150, $four);
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
?>
输出
输出结果如下:


