Skip to content
Related Articles

Related Articles

Improve Article

PHP | Gmagick getimagehistogram() Function

  • Last Updated : 21 Jan, 2020

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:




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

Output:

Returns an array with 2955 Gmagick objects as members.

Program 2:




<?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>";
}
?>

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
Recommended Articles
Page :