PHP | imagecolorat() function

The imagecolorat() function is an inbuilt function in PHP which is used to get the index of the color of the pixel. This function returns the pixel value at the specified location.

Syntax:

int imagecolorat( $image, $x, $y )

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



  • $image: The imagecreatetruecolor() function is used to create an image in a given size.
  • $x: This parameter is used to hold the x-coordinate of the point.
  • $y: This parameter is used to hold the y-coordinate of the point.

Return Value: This function returns the color index (color pixel value) or FALSE on failure.

Below program illustrate the imagecolorat() function in PHP.

Note: The image given below is used in the following program.
geeks image

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// store the image in variable
$image = imagecreatefrompng("gfg.png");
  
// Calculate rgb pixel value at perticular point.
$rgb = imagecolorat($image, 30, 25);
$red = ($rgb >> 16) & 255;
$green = ($rgb >> 8) & 255;
$blue = $rgb & 255;
  
var_dump($red, $green, $blue);
?>

chevron_right


Output:

int(34) 
int(170) 
int(66)

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// store the image in variable.
$image = imagecreatefrompng("gfg.png");
  
// 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);
?>

chevron_right


Output:

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

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.