Skip to content
Related Articles

Related Articles

PHP | imagecolorsforindex() Function
  • Last Updated : 23 Aug, 2019

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
   
// store the image in variable.
$image = imagecreatefrompng(
   
// Calculate rgb pixel value at perticular 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 perticular 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

My Personal Notes arrow_drop_up
Recommended Articles
Page :