Open In App

PHP | imagecolorsforindex() Function

The imagecolorsforindex() function is an inbuilt function in PHP which is used to get the colors at the given index. This function returns an array containing red, green, blue and alpha key values for specified color value.
Syntax: 
 

array imagecolorsforindex ( $image, $index )

Parameters: This function accepts two parameters as mentioned above and described below: 
 



Return Value: This function returns an associative array containing red, green, blue and alpha keys value at the given index. 
Below programs illustrate the imagecolorsforindex() function in PHP:
Program 1: 
 




<?php
  
// store the image in variable.
$image = imagecreatefrompng(
  
// Calculate rgb pixel value at particular point.
$rgb = imagecolorat($image, 30, 25);
  
// Assign color name and its value.
$colors = imagecolorsforindex($image, $rgb);
  
var_dump($colors);
?>

Output: 
 



array(4) { 
    ["red"]=> int(34) 
    ["green"]=> int(170) 
    ["blue"]=> int(66) 
    ["alpha"]=> int(0) 
} 

Program 2: 
 




<?php
  
// store the image in variable.
$image = imagecreatefrompng(
  
// index value
$index_x = 230;
$index_y = 120;
 
// Calculate rgb pixel value at particular point.
$rgba_color = imagecolorat($image, $index_x, $index_y);
 
// Assign color name and its value.
$colors = imagecolorsforindex($image, $rgba_color);
  
var_dump($colors);
?>

Output: 
 

array(4) { 
    ["red"]=> int(97) 
    ["green"]=> int(57) 
    ["blue"]=> int(104) 
    ["alpha"]=> int(0) 
} 

Related Articles: 
 

Reference: http://php.net/manual/en/function.imagecolorsforindex.php
 


Article Tags :