Open In App

PHP | imagecolorsforindex() Function

Improve
Improve
Like Article
Like
Save
Share
Report

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: 
 

  • $image: The imagecreatetruecolor() function is used to create an image in a given size. This function creates blank image of given size.
  • $index: This parameter is used to specify the color index.

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




<?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




<?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
 



Last Updated : 04 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads