Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

PHP | imagecolorat() function

  • Last Updated : 04 Jul, 2021

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: 
 



php




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

Output
 

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

Program 2: 
 

php




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

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!