$val) $total+=$val->amount; //Set the image width and height: the pie will be 300x300, the legend on the right $width = 500; $height = 300; //Create the image resource $image = ImageCreate($width, $height); $n=0; //Define a few colors, and an array of colors $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0); $color[$n++] = imagecolorallocate($image, 255, 0, 0); //red $color[$n++] = imagecolorallocate($image, 0, 0, 255); //blue $color[$n++] = imagecolorallocate($image,255, 255, 0); //yellow $color[$n++] = imagecolorallocate($image,0, 255, 0); //green $color[$n++] = imagecolorallocate($image,255, 255, 0); //magenta $color[$n++] = imagecolorallocate($image,0, 255, 255); //aqua imagefill($image, 0, 0, $white); //Make the background white $legendX=320; $legendY=20; $index=0; $angleStart=0; imagestring($image,5, $legendX, 0, 'LEGEND', $black); foreach($value as $key=>$val) { $text=$key.' $'.number_format($val->amount,0); $percent=$total/$val->amount; $degrees=360/$percent; $angleEnd=$angleStart+$degrees; imagefilledarc($image,150,150,250,250,$angleStart,$angleEnd,$color[$index],0); imagefilledrectangle($image,$legendX,$legendY,$legendX+10,$legendY+10,$color[$index]); imagerectangle($image,$legendX,$legendY,$legendX+10,$legendY+10,$black); //border around the square imagestring($image,3, $legendX+13, $legendY, $text, $black); $legendY+=15; $index++; $angleStart=$angleEnd; } imageellipse($image,150,150,250,250,$black);//Add a black circle around the pie $text='Total: $'.number_format($total,0); imagestring($image,5, $legendX, $legendY+10,$text , $black); // header("Content-Type: image/jpeg"); //Tell browser filetype imagejpeg($image); //Output the image as a jpeg imagedestroy($image); //Free up resources } ?>