PHP | imagecolorexact() Function

The imagecolorexact() function is an inbuilt function in PHP which is used to get the index of specified color in the palette of the image. In created image files only used colors in the image are resolved. Colors present only in the palette are not resolved.

Syntax:

int imagecolorexact ( $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 index of the specified color in the palette on success, or -1 if the color does not exist.

Below programs illustrate the imagecolorexact() function in PHP.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Set the image into variable
$image = imagecreatefrompng(
  
// Create an array containing colors and image
$colors = Array();
$colors[] = imagecolorexact($image, 10, 15, 20);
$colors[] = imagecolorexact($image, 30, 180, 70);
$colors[] = imagecolorexact($image, 12, 55, 25);
$colors[] = imagecolorexact($image, 154, 25, 52);
  
print_r($colors);
  
// Free from memory
imagedestroy($image);
  
?>

chevron_right


Output:

Array ( 
    [0] => 659220 
    [1] => 2012230 
    [2] => 800537 
    [3] => 10098996 
)

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Set the image into variable
$image = imagecreatefrompng(
   
// Create an array containing colors and image
$colors   = Array(
    $colors[] = imagecolorexact($image, 0, 153, 0),
    $colors[] = imagecolorexact($image, 0, 0, 0),
    $colors[] = imagecolorexact($image, 255, 255, 255),
    $colors[] = imagecolorexact($image, 100, 100, 52)
);
   
print_r($colors);
   
// Free from memory
imagedestroy($image);
  
?>

chevron_right


Output:

Array ( 
    [0] => 39168 
    [1] => 0 
    [2] => 16777215 
    [3] => 6579252 
)

Related Articles:

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