Tuesday, October 7, 2025
HomeLanguagesPHP | GmagickPixel getcolorcount() Function

PHP | GmagickPixel getcolorcount() Function

The GmagickPixel::getcolorcount() function is an inbuilt function in PHP which is used to get the color count associated with the pixel color. A color count is the number of pixels in the image that have the same color as this GmagickPixel. The getcolorcount() appears to only work for GmagickPixel objects created through getimagehistogram() function.

Syntax:

int GmagickPixel::getcolorcount( void )

Parameters: This function doesn’t accept any parameters.

Return Value: This function returns an integer containing the color count.

Exceptions: This function throws GmagickPixelException on error.

Below given programs illustrate the GmagickPixel::getcolorcount() function in PHP:

Used Image:

Program 1:




<?php
  
// Create a new Gmagick object
$gmagick = new Gmagick('neveropen.png');
  
// Get the image histogram (image as array of gmagickpixels)
$histogramElements = $gmagick->getImageHistogram(); 
    
// Get the last index 
$lastIndex = count($histogramElements) - 1; 
    
// Get the last element from array which is  
// a gmagickPixel object 
$lastColor = $histogramElements[$lastIndex]; 
    
// Get the Color count 
echo $lastColor->getcolorcount(); 
?>


Output:

100064

Program 2:




<?php
  
// Create a new Gmagick object
$gmagick = new Gmagick('neveropen.png');
  
// Get the image histogram (image as array of gmagickpixels)
$histogramElements = $gmagick->getImageHistogram(); 
  
// Get the first element from array which is  
// a gmagickPixel object 
$firstColor = $histogramElements[0]; 
    
// Get the Color count 
echo $firstColor->getcolorcount(); 
?>


Output:

1

Program 3:




<?php
  
// Create a new Gmagick object
$gmagick = new Gmagick('neveropen.png');
  
// Get the image histogram (image as array of gmagickpixels)
$histogramElements = $gmagick->getImageHistogram(); 
  
// Get the whole color stats 
echo "R G B Hue :Count<br>"; 
foreach ($histogramElements as $pixel) { 
    $colors = $pixel->getcolor(true); 
    foreach ($colors as $color) { 
        print($color . " "); 
    } 
    print(":" . $pixel->getcolorcount() . "<br>"); 
} 
?>


Output:

R G B Hue :Count
0 22 35 :1
0 24 37 :1
0 25 37 :1
0 31 43 :1
0 32 44 :1
0 33 45 :1
0 36 48 :1
0 37 49 :3
.
.
.

Reference: https://www.php.net/manual/en/gmagickpixel.getcolorcount.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
32340 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6708 POSTS0 COMMENTS
Nicole Veronica
11872 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6829 POSTS0 COMMENTS
Ted Musemwa
7090 POSTS0 COMMENTS
Thapelo Manthata
6780 POSTS0 COMMENTS
Umr Jansen
6784 POSTS0 COMMENTS