PHP 中的 imagecolorallocate() 函数

phpserver side programmingprogramming更新于 2025/7/6 8:37:17

imagecolorallocate() 函数用于为图像分配颜色。

语法

int imagecolorallocate ( $img, $red, $green, $blue )

参数

  • img: 使用 imagecreatetruecolor() 创建的图像资源。

  • red: 红色成分

  • green: 绿色成分

  • blue: 蓝色成分

返回

imagecolorallocate() 函数返回 RGB 颜色格式。

示例

以下是一个示例:

<?php
   $img = imagecreatetruecolor(600, 220);
   $bgcolor = imagecolorallocate($img, 20, 50, 120);
   imagefill($img, 0, 0, $bgcolor);
   header("Content-type: image/png");
   imagepng($img);
   imagedestroy($img);
?>

输出

输出结果如下:


相关文章