1--TEST--
2Test Imagick, getImageHistogram
3--SKIPIF--
4<?php
5require_once(dirname(__FILE__) . '/skipif.inc');
6checkFormatPresent('png');
7?>
8--FILE--
9<?php
10
11
12function getColorStatistics($histogramElements, $colorChannel) {
13    $colorStatistics = array();
14
15    foreach ($histogramElements as $histogramElement) {
16        //So broken. Wow. Much surprise. Sad. Bad. Sad, bad, sad.
17        //$color = $histogramElement->getColorValueQuantum($colorChannel);
18        $color = $histogramElement->getColorValue($colorChannel);
19        $color = intval($color * 255);
20        $count = $histogramElement->getColorCount();
21
22        if (array_key_exists($color, $colorStatistics)) {
23            $colorStatistics[$color] += $count;
24        }
25        else {
26            $colorStatistics[$color] = $count;
27        }
28    }
29
30    ksort($colorStatistics);
31
32    return $colorStatistics;
33}
34
35
36
37function getImageHistogram() {
38
39    $backgroundColor = 'black';
40
41    $draw = new \ImagickDraw();
42    $draw->setStrokeWidth(0); //Lines have a wi
43
44    $imagick = new \Imagick();
45    $imagick->newImage(500, 500, $backgroundColor);
46    $imagick->setImageFormat("png");
47    $imagick->drawImage($draw);
48
49    $histogramWidth = 256;
50    $histogramHeight = 100; // the height for each RGB segment
51
52    $imagick = new \Imagick();
53    $imagick->newPseudoImage(640, 480, "magick:logo");
54    //Resize the image to be small, otherwise PHP tends to run out of memory
55    //This might lead to bad results for images that are pathologically 'pixelly'
56    $imagick->adaptiveResizeImage(200, 200, true);
57    $histogramElements = $imagick->getImageHistogram();
58
59    $histogram = new \Imagick();
60    $histogram->newpseudoimage($histogramWidth, $histogramHeight * 3, 'xc:black');
61    $histogram->setImageFormat('png');
62
63    $getMax = function ($carry, $item)  {
64        if ($item > $carry) {
65            return $item;
66        }
67        return $carry;
68    };
69
70    $colorValues = array(
71        'red' => getColorStatistics($histogramElements, \Imagick::COLOR_RED),
72        'lime' => getColorStatistics($histogramElements, \Imagick::COLOR_GREEN),
73        'blue' => getColorStatistics($histogramElements, \Imagick::COLOR_BLUE),
74    );
75
76    $max = array_reduce($colorValues['red'] , $getMax, 0);
77    $max = array_reduce($colorValues['lime'] , $getMax, $max);
78    $max = array_reduce($colorValues['blue'] , $getMax, $max);
79
80    $scale =  $histogramHeight / $max;
81
82    $count = 0;
83    foreach ($colorValues as $color => $values) {
84        $draw->setstrokecolor($color);
85
86        $offset = ($count + 1) * $histogramHeight;
87
88        foreach ($values as $index => $value) {
89            $draw->line($index, $offset, $index, $offset - ($value * $scale));
90        }
91        $count++;
92    }
93
94    $histogram->drawImage($draw);
95    $bytes = $histogram->getImageBlob();
96    if (strlen($bytes) <= 0) { echo "Failed to generate image.";}
97}
98
99getImageHistogram();
100echo "Ok";
101?>
102--EXPECTF--
103Ok