Thursday, September 4, 2025
HomeLanguagesPHP | Gmagick getimagehistogram() Function

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:




<?php
  
// Create a new Gmagick object
$gmagick = new Gmagick('neveropen.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('neveropen.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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32263 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11857 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6696 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS