$thejpgfile = $_GET["jpgfile"]; 
?>
";
$index = 0;
foreach ($array as $d) {
echo "index $index: ";
echo "$d
";
++$index;
}
function ensure_bounds ( &$val )
{
	$val = round( $val );
	if ( $val < 0 ) $val = 0;
	elseif ( $val > 255 ) $val = 255;			
}
function create_rgb_variable( $r, $g, $b )
{
	$rgb_variable = sprintf( "%02x%02x%02x", $r, $g, $b);
	return $rgb_variable;
}
		
function calculate_colors( $jpgfile )
{        
 	$filename = $jpgfile;
 	$color_interval = 32;
 	
 	$image = imagecreatefromjpeg( $jpgfile );  
if ($image) { /* See if it failed */
echo "imagecreatefromjpeg returned an image id: $image 
";
}
else
{
echo "imagecreatefromjpeg returned a null: $image FAILED!
";
}
 	$primary = "000000";
 	$secondary = "000000";
 	$tertiary = "000000";
 	$secondarydarkest = "000000";
 	$tertiarylighest = "000000";
	$fourlighest = "000000";
	$fivelighest = "000000";
	$sixlighest = "000000";
	$sevenlighest = "000000";
echo "before big if block
"; 	
	if ( $image ) 
	{
echo "inside big if block
"; 
		//$dim = getimagesize( $image );
$xdim = imagesx($image);
$ydim = imagesy($image);
echo "xdim: $xdim, ydim: $ydim 
"; 
	 	$rgb = imageColorAt( $image, 0, 0 );
		$total_pixels = 0;
		$primary_r = 0;
		$primary_g = 0;
		$primary_b = 0;
			
		for ( $x = 0; $x < $xdim; $x++ )
		{
 
			for ( $y = 0; $y < $ydim; $y++ )
			{
				$rgb = ImageColorAt( $image, $x, $y );
				$tval = ( ($rgb >> 16) & 0xFF );
				$primary_r += $tval;
					
				$tval = ( ($rgb >> 8) & 0xFF );
				$primary_g += $tval;
				$tval = ( $rgb & 0xFF );
				$primary_b += $tval;
				++$total_pixels;
			}
		}
		$primary_r /= $total_pixels;
 		$primary_g /= $total_pixels;
 		$primary_b /= $total_pixels;
			
		ensure_bounds( $primary_r );
		ensure_bounds( $primary_g );
		ensure_bounds( $primary_b );
		$primary = create_rgb_variable( $primary_r, $primary_g, $primary_b );
		$secondary_r = $primary_r - ( $color_interval * 1.5 );
		$secondary_g = $primary_g - ( $color_interval * 1.5 );
		$secondary_b = $primary_b - ( $color_interval * 1.5 );
		ensure_bounds( $secondary_r );
		ensure_bounds( $secondary_g );
		ensure_bounds( $secondary_b );
		$secondary = create_rgb_variable( $secondary_r, $secondary_g, $secondary_b );
 			
		$tertiary_r = $primary_r + $color_interval * 0.5;
		$tertiary_g = $primary_g + $color_interval * 0.5;
		$tertiary_b = $primary_b + $color_interval * 0.5;
		ensure_bounds( $tertiary_r );
		ensure_bounds( $tertiary_g );
		ensure_bounds( $tertiary_b );
		$tertiary = create_rgb_variable( $tertiary_r, $tertiary_g, $tertiary_b );
                $secondarydarkest_r = $primary_r - ( $color_interval * 2.5 );
		$secondarydarkest_g = $primary_g - ( $color_interval * 2.5 );
		$secondarydarkest_b = $primary_b - ( $color_interval * 2.5 );
		ensure_bounds( $secondarydarkest_r );
		ensure_bounds( $secondarydarkest_g );
		ensure_bounds( $secondarydarkest_b );
		$secondarydarkest = create_rgb_variable( $secondarydarkest_r, $secondarydarkest_g, $secondarydarkest_b );
                $tertiarylighest_r = $primary_r + ( $color_interval * 1.5 );
		$tertiarylighest_g = $primary_g + ( $color_interval * 1.5 );
		$tertiarylighest_b = $primary_b + ( $color_interval * 1.5 );
		ensure_bounds( $tertiarylighest_r );
		ensure_bounds( $tertiarylighest_g );
		ensure_bounds( $tertiarylighest_b );
		$tertiarylighest = create_rgb_variable( $tertiarylighest_r, $tertiarylighest_g, $tertiarylighest_b );
		$fourlighest_r = $primary_r + ( $color_interval * 2.0 );
		$fourlighest_g = $primary_g + ( $color_interval * 2.0 );
		$fourlighest_b = $primary_b + ( $color_interval * 2.0);
		ensure_bounds( $fourlighest_r );
		ensure_bounds( $fourlighest_g );
		ensure_bounds( $fourlighest_b );
		$fourlighest = create_rgb_variable( $fourlighest_r, $fourlighest_g, $fourlighest_b );
		$fivelighest_r = $primary_r + ( $color_interval * 2.5 );
		$fivelighest_g = $primary_g + ( $color_interval * 2.5 );
		$fivelighest_b = $primary_b + ( $color_interval * 2.5 );
		ensure_bounds( $fivelighest_r );
		ensure_bounds( $fivelighest_g );
		ensure_bounds( $fivelighest_b );
		$fivelighest = create_rgb_variable( $fivelighest_r, $fivelighest_g, $fivelighest_b );
		$sixlighest_r = $primary_r + ( $color_interval * 3.0 );
		$sixlighest_g = $primary_g + ( $color_interval * 3.0 );
		$sixlighest_b = $primary_b + ( $color_interval * 3.0 );
		ensure_bounds( $sixlighest_r );
		ensure_bounds( $sixlighest_g );
		ensure_bounds( $sixlighest_b );
		$sixlighest = create_rgb_variable( $sixlighest_r, $sixlighest_g, $sixlighest_b );
		$sevenlighest_r = $primary_r + ( $color_interval * 3.5 );
		$sevenlighest_g = $primary_g + ( $color_interval * 3.5 );
		$sevenlighest_b = $primary_b + ( $color_interval * 3.5 );
		ensure_bounds( $sevenlighest_r );
		ensure_bounds( $sevenlighest_g );
		ensure_bounds( $sevenlighest_b );
		$sevenlighest = create_rgb_variable( $sevenlighest_r, $sevenlighest_g, $sevenlighest_b );
	}
	
	return array( $primary, $secondary, $tertiary, $secondarydarkest, $tertiarylighest, $fourlighest, $fivelighest, $sixlighest, $sevenlighest );
}
?>