shop-old/libs/phImaginator/phImaginator.Measure.class.php
2026-04-20 01:03:43 +02:00

127 lines
4.3 KiB
PHP

<?php
abstract class phImaginator_Measure extends phImaginator_Universal implements phImaginator_Module {
protected static $runCollected = false;
public static function histogram() {
$image_data =& parent::getCurrentResource();
$w = $image_data['width'];
$h = $image_data['height'];
$resource =& $image_data['resource'];
$colorcount = array('rgb' => array(), 'r' => array(), 'g' => array(), 'b' => array());
$colorSums = array('rgb' => 0, 'r' => 0, 'g' => 0, 'b' => 0);
for ($x = 0; $x < $w; ++$x) {
for ($y = 0; $y < $h; ++$y) {
$rgb = imagecolorat($resource, $x, $y);
$t = ($rgb >> 24) & 0xFF;
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
++$colorcount['rgb'][($r + $g + $b) / 3];
++$colorcount['r'][$r];
++$colorcount['g'][$g];
++$colorcount['b'][$b];
}
}
$maxColors = max(array(max($colorcount['rgb']), max($colorcount['r']), max($colorcount['g']), max($colorcount['b'])));
$infoHeight = 12;
$pixelsPerColorStep = (($h - 1) - $infoHeight * 2) / $maxColors;
$pixelsPerColor = ($w - 1) / 256;
$overlay = imagecreatetruecolor($w, $h);
imagefill($overlay, 0, 0, imagecolorallocate($overlay, 0, 0, 0));
imagecopymerge($resource, $overlay, 0, 0, 0, 0, $w, $h, 80);
$rColor = imagecolorallocatealpha($resource, 255, 0, 0, 80);
$gColor = imagecolorallocatealpha($resource, 0, 255, 0, 80);
$bColor = imagecolorallocatealpha($resource, 0, 0, 255, 80);
$grayColor = imagecolorallocatealpha($resource, 255, 255, 255, 63);
$points = array(0, $h - 1 - $infoHeight);
for ($c = 0; $c <= 255; ++$c) {
$points[] = $c * $pixelsPerColor + 1;
$points[] = ($h - 1 - $infoHeight) - $colorcount['rgb'][$c] * $pixelsPerColorStep - 1;
imagefilledrectangle($resource, $c * $pixelsPerColor, $h - 1 - $infoHeight, $c * ($pixelsPerColor * 2), $h - 1, imagecolorallocate($resource, $c, $c, $c));
}
$points[] = $w;
$points[] = $h - 1 - $infoHeight;
imagefilledpolygon($resource, $points, count($points) / 2, $grayColor);
for ($c = 0; $c <= 255; ++$c) {
imageline(
$resource,
$c * $pixelsPerColor + 1,
($h - 1 - $infoHeight) - $colorcount['r'][$c] * $pixelsPerColorStep - 1,
($c - 1) * $pixelsPerColor + 1,
($h - 1 - $infoHeight) - $colorcount['r'][$c - 1] * $pixelsPerColorStep - 1,
$rColor
);
imagefilledrectangle($resource, $c * $pixelsPerColor, 0, $c * ($pixelsPerColor * 2), $infoHeight / 3 - 1, imagecolorallocate($resource, $c, 0, 0));
imageline(
$resource,
$c * $pixelsPerColor + 1,
($h - 1 - $infoHeight) - $colorcount['g'][$c] * $pixelsPerColorStep - 1,
($c - 1) * $pixelsPerColor + 1,
($h - 1 - $infoHeight) - $colorcount['g'][$c - 1] * $pixelsPerColorStep - 1,
$gColor
);
imagefilledrectangle($resource, $c * $pixelsPerColor, $infoHeight / 3, $c * ($pixelsPerColor * 2), ($infoHeight / 3) * 2 - 1, imagecolorallocate($resource, 0, $c, 0));
imageline(
$resource,
$c * $pixelsPerColor + 1,
($h - 1 - $infoHeight) - $colorcount['b'][$c] * $pixelsPerColorStep - 1,
($c - 1) * $pixelsPerColor + 1,
($h - 1 - $infoHeight) - $colorcount['b'][$c - 1] * $pixelsPerColorStep - 1,
$bColor
);
imagefilledrectangle($resource, $c * $pixelsPerColor, ($infoHeight / 3) * 2, $c * ($pixelsPerColor * 2), $infoHeight - 1, imagecolorallocate($resource, 0, 0, $c));
$colorSums['rgb'] += $c * $colorcount['rgb'][$c];
$colorSums['r'] += $c * $colorcount['r'][$c];
$colorSums['g'] += $c * $colorcount['g'][$c];
$colorSums['b'] += $c * $colorcount['b'][$c];
}
$rgbAverage = ($colorSums['rgb'] / array_sum($colorcount['rgb'])) * $pixelsPerColor + 1;
imageline($resource, $rgbAverage, $infoHeight, $rgbAverage, ($h - $infoHeight * 2) / 4, $grayColor);
$rAverage = ($colorSums['r'] / array_sum($colorcount['r'])) * $pixelsPerColor + 1;
imageline($resource, $rAverage, ($h - $infoHeight * 2) / 4, $rAverage, (($h - $infoHeight * 2) / 4) * 2, $rColor);
$gAverage = ($colorSums['g'] / array_sum($colorcount['g'])) * $pixelsPerColor + 1;
imageline($resource, $gAverage, (($h - $infoHeight * 2) / 4) * 2, $gAverage, (($h - $infoHeight * 2) / 4) * 3, $gColor);
$bAverage = ($colorSums['b'] / array_sum($colorcount['b'])) * $pixelsPerColor + 1;
imageline($resource, $bAverage, (($h - $infoHeight * 2) / 4) * 3, $bAverage, $h - $infoHeight - 1, $bColor);
}
}
?>