PHP Function Imagecolorsforindex
PHP makes it very easy to do many of the things that are needed on a website. This includes the ability to create images in a number of formats including gif, png, jpg, swf, wbmp and xpm. You can also use PHP to work with images that are stored on the web server in a variety of ways from reading information in the image header, through to adding text or drawing lines to the picture. This article covers the php function imagecolorsforindex which is used to get the colours for an index in an existing image.
The imagecolorsforindex() function takes four arguments. These are a resource that represents the image, a reference to the canvas, the red, green and blue values for that particular index in the image. The return value is an associative array containing the red, green, blue and alpha keys for that index in the image.
This is a simple function and the snippet of code below shows how it works. Note that the pixel values are stored in the variables as integers not floating point numbers, this is because PHP will round the values to prevent overflows.
The first time you call the function it will set the background of the image to a specific colour - in this case a shade of blue. However, subsequent calls to the function will just allocate a colour identifier for that colour without altering the background of the image.