# Farbanteile ermitteln
Laden Sie ein Bild hoch und Sie erhalten eine detaillierte Auflistung aller Farben im Bild und ihrer Häufigkeit.
# Wie funktioniert es?
Jeder Pixel besitzt drei Farben, aus denen er sich zusammensetzt, wenn wir von RGB ausgehen.

Wir ermitteln mithilfe einer Schleife (genau genommen zwei, für x und y) für jeden Pixel die drei RGB-Werte und schreiben sie in ein Array, welches außerdem noch mitzählt, wie oft eine Farbe vorkommt. Am Ende generieren wir einen schönen String $str, welcher in eine Datei geschrieben wird. Alternativ kann man natürlich das Script so umbauen, dass direkt eine Vorschau für jede Farbe gezeigt wird. Doch das verursacht für den Browser, der es anzeigt, eine ernorme Rechenlast, welche bei heute handelsüblichen Computern zum Absturz führt. Für ein solches Experiment sollte ein smarter, verbrauchsarmer Browser verwendet werden, der nicht so viel Speicher frisst. Zu diesem Zwecke und zum Surfen unter älteren Computern kann ich Google Chrome empfehlen.
# Das Script
<?php
/*
Copyright (c) 2010 <Max Großmann>
Permission is hereby granted, free of charge,
to any person obtaining a copy of this software
and associated documentation files (the "Software"),
to deal in the Software without restriction,
including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to
whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
$bildurl = "abc.png";
$im = imagecreatefrompng($bildurl);
$gp = getimagesize($bildurl);
$cl = array();
$width = $gp[0];
$height = $gp[1];
for($px_y=0;$px_y<$height;$px_y++) {
for($px_x=0;$px_x<$width;$px_x++) {
$color = imagecolorat($im, $px_x, $px_y);
$color = imagecolorsforindex($im, $color);
$cf = $color["red"].",".$color["green"].",".$color["blue"];
if (isset($cl[$cf])) {
$cl[$cf]++;
}
else {
$cl[$cf] = 1;
}
}
}
imagedestroy($im);
natsort($cl);
$cl = array_reverse($cl);
$ges = $width*$height;
$str = $ges." Pixel\r\n";
$o = 0;
foreach ($cl as $i=>$v) {
$rrr = explode(",",$i);
$rev = (255-$rrr[0]).",".(255-$rrr[1]).",".(255-$rrr[2]);
unset($rrr);
$str .= "$v* RGB($i)\r\n";
unset($exp);
$o += $v;
}
$fl = md5(sha1(crypt(rand(0,5000).time())));
file_put_contents("$fl-analyse.txt",$str);
?>

