Imagick::getImageHistogram

(PECL imagick 2, PECL imagick 3)

Imagick::getImageHistogramGets the image histogram

Description

public Imagick::getImageHistogram(): array

Returns the image histogram as an array of ImagickPixel objects.

Parameters

This function has no parameters.

Return Values

Returns the image histogram as an array of ImagickPixel objects.

Errors/Exceptions

Throws ImagickException on error.

Examples

Example #1 Generates Imagick::getImageHistogram()

<?php
function getColorStatistics($histogramElements$colorChannel) {
    
$colorStatistics = [];

    foreach (
$histogramElements as $histogramElement) {
        
$color $histogramElement->getColorValue($colorChannel);
        
$color intval($color 255);
        
$count $histogramElement->getColorCount();

        if (
array_key_exists($color$colorStatistics)) {
            
$colorStatistics[$color] += $count;
        }
        else {
            
$colorStatistics[$color] = $count;
        }
    }

    
ksort($colorStatistics);
    
    return 
$colorStatistics;
}
    


function 
getImageHistogram($imagePath) {

    
$backgroundColor 'black';

    
$draw = new \ImagickDraw();
    
$draw->setStrokeWidth(0); //make the lines be as thin as possible

    
$imagick = new \Imagick();
    
$imagick->newImage(500500$backgroundColor);
    
$imagick->setImageFormat("png");
    
$imagick->drawImage($draw);

    
$histogramWidth 256;
    
$histogramHeight 100// the height for each RGB segment

    
$imagick = new \Imagick(realpath($imagePath));
    
//Resize the image to be small, otherwise PHP tends to run out of memory
    //This might lead to bad results for images that are pathologically 'pixelly'
    
$imagick->adaptiveResizeImage(200200true);
    
$histogramElements $imagick->getImageHistogram();

    
$histogram = new \Imagick();
    
$histogram->newpseudoimage($histogramWidth$histogramHeight 3'xc:black');
    
$histogram->setImageFormat('png');

    
$getMax = function ($carry$item)  {
        if (
$item $carry) {
            return 
$item;
        }
        return 
$carry;
    };

    
$colorValues = [
        
'red' => getColorStatistics($histogramElements, \Imagick::COLOR_RED),
        
'lime' => getColorStatistics($histogramElements, \Imagick::COLOR_GREEN),
        
'blue' => getColorStatistics($histogramElements, \Imagick::COLOR_BLUE),
    ];

    
$max array_reduce($colorValues['red'] , $getMax0);
    
$max array_reduce($colorValues['lime'] , $getMax$max);
    
$max array_reduce($colorValues['blue'] , $getMax$max);

    
$scale =  $histogramHeight $max;

    
$count 0;
    foreach (
$colorValues as $color => $values) {
        
$draw->setstrokecolor($color);

        
$offset = ($count 1) * $histogramHeight;

        foreach (
$values as $index => $value) {
            
$draw->line($index$offset$index$offset - ($value $scale));
        }
        
$count++;
    }

    
$histogram->drawImage($draw);
    
    
header"Content-Type: image/png" );
    echo 
$histogram;
}

?>

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

Was genau bedeutet "Vibe Coding"? Ein tiefgehender Blick für Entwickler

In der Welt der Softwareentwicklung gibt es unzählige Wege, wie man an ein Projekt herangeht. Manche schwören auf strikte Planung, andere auf bewährte Algorithmen und wieder andere lassen sich von etwas ganz anderem leiten: ihrem Gefühl. ...

admin

Autor : admin
Category: Software & Web-Development

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial