PHP imagecolorallocate - Allocate a color for an image

PHP 图像处理PHP Image Processing

imagecolorallocate — Allocate a color for an image.

Syntax

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

imagecolorallocate() returns an identifier representing a color composed of the given RGB components. red, green, and blue are the red, green, and blue components of the desired color, respectively. These parameters are integers from 0 to 255 or hexadecimal 0x00 to 0xFF. imagecolorallocate() must be called to create each color used in the image represented by image.

Returns -1 if the allocation fails.

Note:The first call to imagecolorallocate() fills the background color of a palette-based image, i.e., the color used whenimagecreate()creating the image.

Example

<?php
header("Content-type: image/png");
$im = @imagecreate(100, 50)
    or die("不能初始化新的 GD 图像流");
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>

The output image of the above example is as follows:

Related Articles

PHP 图像处理PHP Image Processing

Other Extensions