This article describes how to use the imagecolorallocate () function to set the color for PHP image processing. This article provides two ways to set the color in decimal and hexadecimal notation, for more information about how to use PHP to dynamically output beautiful images, see the color settings. To set the image color, call the imagecolorallocate () function. If you need to set multiple colors in the image, you only need to call this function multiple times. The function is prototype as follows:
The code is as follows:
Int imagecolorallocate (resource $ image, int $ red, int $ green, int $ blue) // assign color to an image
This function returns an identifier representing the color composed of a given RGB component. The $ red, $ green, and $ blue parameters are the red and green colors required. These parameters are integers from 0 to 255 or hexadecimal values 0 × 00 to 0xFF. The 1st parameter $ image is the handle of the canvas image. this function must call the color in the image represented by $ image. However, if you use the imagecreate () function to create a canvas, the first call to the imagecolorallocate () function will fill the background color with the image based on the color palette. The code for using this function is as follows:
The code is as follows:
<? Php
$ Im = imagecreate (100,100); // provides a canvas resource for the color setting function.
// Set the background to red
$ Background = imagecolorallocate ($ m, 255, 0, 0); // The first call sets the background color for the canvas.
// Set some colors
$ White = imagecolorallocate ($ im, 255,255,255); // returns an identifier that is white as a decimal integer.
$ Black = imagecolorallocate ($ im, 0); // return the identifier set to black by the decimal parameter
// Hexadecimal mode
$ White = imagecolorallocate ($ im, 0xFF, 0xFF, 0xFF); // returns an identifier that is white as a hexadecimal integer.
$ Black = imagecolorallocate ($ im, 0 x, 0 x, 0x00); // returns an identifier that is set to black by a hexadecimal integer.
?>