PHP | Gmagick getimagehistogram() Function

The Gmagick::getimagehistogram() function is an inbuilt function in PHP which is used to get the image histogram. This function returns all the pixels from the picture in the form of an array of Gmagick pixels. We can use this function to analyze the color of any picture pixel by pixel.

Syntax:

array Gmagick::getimagehistogram( void )

Parameters: This function doesn’t accept any parameters.



Return Value: This function returns an array value containing the histogram.

Exceptions: This function throws GmagickException on error.

Below given programs illustrate the Gmagick::getimagehistogram() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new Gmagick object
$gmagick = new Gmagick('geeksforgeeks.png');
  
// Get the histogram
$histogram = $gmagick->getimagehistogram();
print("<pre>".print_r($histogram, true)."</pre>");
?>

chevron_right


Output:

Returns an array with 2955 Gmagick objects as members.

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new Gmagick object
$gmagick = new Gmagick('geeksforgeeks.png');
  
// Get the histogram
$histogram = $gmagick->getimagehistogram();
  
echo "Color of first five pixels are <br>";
for ($i = 0; $i < 5; $i++) {
    // Get the color of ith pixel
    $color = $histogram[$i]->getcolor();
    echo $color . "<br>";
}
?>

chevron_right


Output:

Color of first five pixels are
rgb(0, 5654, 8995)
rgb(0, 6168, 9509)
rgb(0, 6425, 9509)
rgb(0, 7967, 11051)
rgb(0, 8224, 11308)

Reference: https://www.php.net/manual/en/gmagick.getimagehistogram.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.