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:
- $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
$image = imagecreatefrompng(
$rgb = imagecolorat( $image , 30, 25);
$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
$image = imagecreatefrompng(
$index_x = 230;
$index_y = 120;
$rgba_color = imagecolorat( $image , $index_x , $index_y );
$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
Share your thoughts in the comments
Please Login to comment...