Page 1 of 1

trying to find the most outstanding (average) RGB in a jpg

Posted: Wed Oct 18, 2006 9:45 am
by webdeconstruction
I'm trying to find the most outstanding RGB values in a jpg but I'm having deep troubles, I also don't know how possible is this within my knowledge of php, any help would be greatly appreciated:

Code: Select all

<?php

// open an image

$im2 = imagecreatefromjpeg('example.jpg');

// make truecolor to web safe
$im = imagetruecolortopalette($im2, false, 255);

$imsiz = getimagesize('example.jpg');
$imx = $imsiz[0];
$imy = $imsiz[1];

$totalPixels = $imy+$imx;
print($totalPixels.'JJJJ');

for($y = 0; $y < $imy ;$y++)
{
	for($x = 0; $x <$imx; $x++)
	{
		$color_index = imagecolorat($im, $x, $y);
		$color_tran = imagecolorsforindex($im, $color_index);
		
		for($i=0; $i < $totalPixels; $i++){
		
			$imR[$totalPixels] = array($color_tran[red]);
			$imG[$totalPixels] = array($color_tran[green]);
			$imB[$totalPixels] = array($color_tran[blue]);
		}
	}

}
		
$imRTotal = count($imR);
$imGTotal = count($imG);
$imBTotal = count($imB);

$imRMean = array_sum($imR)/$imRTotal;
$imGMean = array_sum($imG)/$imGTotal;
$imBMean = array_sum($imB)/$imBTotal;
?>