Home  >  Article  >  Backend Development  >  Example of setting color using imagecolorallocate() function in PHP image processing

Example of setting color using imagecolorallocate() function in PHP image processing

高洛峰
高洛峰Original
2016-12-26 12:40:223339browse

While using PHP to dynamically output beautiful images, it is also inseparable from color settings, just like you need to use a palette when painting. To set the color of an image, you need to call the imagecolorallocate() function. If you need to set multiple colors in the image, just call this function multiple times. The prototype of this function is as follows:

  int imagecolorallocate(resource $image,int $red,int $green,int $blue)                //为一幅图分配颜色

This function returns an identifier that represents the color composed of the given RGB components. The parameters $red, $green and $blue are the red, green and blue components of the required color respectively. These parameters are integers from 0 to 255 or hexadecimal from 0x00 to 0xFF. The first parameter $image is the handle of the canvas image. This function must call the color in the image represented by $image. But be aware that if the canvas is created using the imagecreate() function, the first call to the imagecolorallocate() function will fill the background color with the image based on the palette. The usage code of this function is as follows:

<?php
$im = imagecreate(100,100);//为设置颜色函数提供一个画布资源
//背景设为红色
$background = imagecolorallocate($m,255,0,0);//第一次调用即为画布设置背景颜色
//设定一些颜色
$white = imagecolorallocate($im,255,255,255);//返回由十进制整数设置为白色的标识符
$black = imagecolorallocate($im,0,0,0);//返回由十进制参数设置为黑色的标识符
//十六进制方式
$white = imagecolorallocate($im,0xFF,0xFF,0xFF);//返回由十六进制整数设置为白色的标识符
$black = imagecolorallocate($im,0x00,0x00,0x00);//返回由十六进制整数设置为黑色的标识符
?>

For more PHP image processing examples of using the imagecolorallocate() function to set colors, please pay attention to the PHP Chinese website for related articles!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn