PHP imagecolorallocate()和imagecolorallocatealpha():定义颜色

  • 内容
  • 评论
  • 相关

在使用 PHP 动态输出漂亮图像的同时,也离不开颜色的设置,就像画画时需要使用调色板一样。在 PHP 中提供了 imagecolorallocate() 和 imagecolorallocatealpha() 两个函数来设置图像的颜色,下面就来详细介绍一下。

1、imagecolorallocate() 函数

imagecolorallocate() 函数可以为一个图像资源分配颜色,如果在图像中需要设置多种颜色,只要多次调用该函数即可。函数的语法格式如下所示:

imagecolorallocate(resource $image, int $red, int $green, int $blue)

其中,$image 为要设置颜色的图像资源,imagecolorallocate() 函数会返回一个标识符,代表了由给定的 RGB 成分组成的颜色;$red,$green 和 $blue 分别是所需要的颜色的红,绿,蓝成分,取值范围是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。

提示:如果是使用 imagecreate() 函数创建的图像资源,在第一次调用 imagecolorallocate() 函数时会默认为其填充背景色。

【示例】使用 imagecolorallocate() 函数为图像设置颜色。

<?php
    $image = imagecreate(100, 100);
    $blue = imagecolorallocate($image, 0, 0, 255);
    $red = imagecolorallocate($image, 255, 0, 0);
    $green = imagecolorallocate($image, 0, 255, 0);
    header('Content-type:image/jpeg');
    imagejpeg($image);
    imagedestroy($image);
?>

运行结果如下图所示:

imagecolorallocate() 函数示例程序运行结果
图:imagecolorallocate() 函数示例程序运行结果

本文标题:PHP imagecolorallocate()和imagecolorallocatealpha():定义颜色

本文地址:https://www.hosteonscn.com/7262.html

评论

0条评论

发表评论

邮箱地址不会被公开。 必填项已用*标注