PHP | imagecolorresolve() Function

The imagecolorresolve() function is an inbuilt function in PHP which is used to get the index of the specified color or its closest possible alternative color. This function returns a color index value for a requested color, either the exact matched color or the closest possible alternative.

Syntax:

int imagecolorresolve ( $image, $red, $green, $blue )

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



  • $image: It is returned by one of the image creation functions, such as imagecreatetruecolor(). It is used to create size of image.
  • $red: This parameter is used to set value of red color component.
  • $green: This parameter is used to set value of green color component.
  • $blue: This parameter is used to set value of blue color component.

Return Value: This function returns the color index.

Below programs illustrate the imagecolorresolve() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Load an image
$image = imagecreatefromgif(
   
// Get closest colors from the image
$colors = array();
$colors[] = imagecolorresolve($image, 167, 75, 55);
$colors[] = imagecolorresolve($image, 150, 25, 250);
$colors[] = imagecolorresolve($image, 161, 234, 135);
$colors[] = imagecolorresolve($image, 143, 255, 254);
   
// Output
print_r($colors);
   
imagedestroy($image);
?>

chevron_right


Output:

Array ( 
    [0] => 187 
    [1] => 188 
    [2] => 189 
    [3] => 190 
) 

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Load an image
$image = imagecreatefrompng(
   
// Get closest colors from the image
$colors = array(
    imagecolorresolve($image, 156, 0, 255),
    imagecolorresolve($image, 0, 255, 200),
    imagecolorresolve($image, 16, 134, 35),
    imagecolorresolve($image, 143, 255, 254)
);
   
// Output
print_r($colors);
   
imagedestroy($image);
?>

chevron_right


Output:

Array ( 
    [0] => 10223871 
    [1] => 65480 
    [2] => 1082915 
    [3] => 9437182 
) 

Related Articles:

Reference: http://php.net/manual/en/function.imagecolorresolve.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.