Colors distribution of an image

I've created a very simple script in php using GD Library to analyze an image and its colors distribution. You can reimplement this algorithm (very simple) in another language and track your graph.
You can see an example at this page: http://www.st4ck.com/examples/php/rgb.php

The code of the php script:

<?php
header ("Content-type: image/png");
$img = imagecreatefromjpeg($_GET['image']);
$height = imagesy($img);
$width = imagesx($img);
$im = ImageCreate (512, 256);
$white = ImageColorAllocate ($im, 255, 255, 255);
$color[0] = ImageColorAllocate ($im, 0, 0, 255);
$color[1] = ImageColorAllocate ($im, 0, 255, 0);
$color[2] = ImageColorAllocate ($im, 255, 0, 0);
$black = ImageColorAllocate ($im, 0, 0, 0);
$gray = ImageColorAllocate ($im, 200, 200, 200);

if ($_GET['channel'] == "red") {
for ($w=0; $w<$width; $w++) {
for ($h=0; $h<$height; $h++) {

$rgb = imagecolorat($img, $w, $h);
$r[($rgb >> 16) & 0xFF]++;
}
}

$rmax = $r[0];
$gmax = $g[0];
$bmax = $b[0];
for ($i=1; $i<256; $i++) {
if ($r[$i] > $rmax) {
$rmax = $r[$i];
}
}

for ($i=0; $i<256; $i++) {
ImageFilledRectangle($im,$i*2,256,($i+1)*2,256-256*($r[$i]/$rmax), $color[2]);
}
}

if ($_GET['channel'] == "green") {
for ($w=0; $w<$width; $w++) {
for ($h=0; $h<$height; $h++) {

$rgb = imagecolorat($img, $w, $h);
($rgb >> 16) & 0xFF;
$g[($rgb >> 8) & 0xFF]++;
}
}

$gmax = $g[0];
for ($i=1; $i<256; $i++) {
if ($g[$i] > $gmax) {
$gmax = $g[$i];
}
}

for ($i=0; $i<256; $i++) {
ImageFilledRectangle($im,$i*2,256,($i+1)*2,256-256*($g[$i]/$gmax), $color[1]);
}
}

if ($_GET['channel'] == "blue") {
for ($w=0; $w<$width; $w++) {
for ($h=0; $h<$height; $h++) {

$rgb = imagecolorat($img, $w, $h);
($rgb >> 16) & 0xFF;
($rgb >> 8) & 0xFF;
$b[$rgb & 0xFF]++;
}
}

$bmax = $b[0];
for ($i=1; $i<256; $i++) {
if ($b[$i] > $bmax) {
$bmax = $b[$i];
}
}

for ($i=0; $i<256; $i++) {
ImageFilledRectangle($im,$i*2,256,($i+1)*2,256-256*($b[$i]/$bmax), $color[0]);
}
}

ImagePng ($im);
ImageDestroy ($im);
?>


This script has two GET args: first arg is the path of the image to analyze ($_GET['image']) and second arg is the channel that you want as output.

For example, if you want to analyze image /example.jpg and see red color distribution, you have to write in your page: <img src="rgb.php?image=/example.jpg&channel=red" alt="">