Computer >> Computer tutorials >  >> Programming >> PHP

imagecolorsforindex() function in PHP


The imagecolorsforindex() function gets the colors for an index.

Syntax

imagecolorsforindex ( img, index )

Parameters

  • img: Creates an image with imagecreatetruecolor()

  • Index: Set the color index.

Return

The imagecolorsforindex() function returns an associative array with red, green, blue and alpha keys that contain the appropriate values for the specified color index.

Example

The following is an example

<?php
   $img = imagecreatefrompng('https://www.tutorialspoint.com/images/tp-logo-diamond.png');
   $x = 25;
   $y = 45;
   $rgb = imagecolorat($img, 25, 45);
   $colors = imagecolorsforindex($img, $rgb);
   var_dump($colors);
?>

Output

The following is the output:

array(4) { ["red"]=> int(255) ["green"]=> int(255) ["blue"]=> int(255) ["alpha"]=> int(127) }