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:




<?php
   
// Load an image
$image = imagecreatefromgif(
'https://media.w3wiki.net/wp-content/uploads/animateImages.gif');
   
// 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);
?>


Output:

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

Program 2:




<?php
   
// Load an image
$image = imagecreatefrompng(
'https://media.w3wiki.net/wp-content/uploads/col1.png');
   
// 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);
?>


Output:

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

Related Articles:

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



Contact Us