<?php
/*-------------------------------
Script gemaakt door D@rk_force
Email: Nobody6@telenet.be
Voorbeeld: 
Sites Gepubliceerd:
http://www.phphulp.nl
http://www.tweakzone.nl
http://www.phpfreakz.nl
http://www.phpv.nl
http://flaming.lumix.nl
-------------------------------*/
error_reporting(E_ALL);
//Diameter van de cirkel
$conf['diameter']=300;
//Breedte legende, moet breder worden als de resultaten er niet op kunnen
$conf['legende']=300;
//Hoe breed is de rand van de cirkel
$conf['breedte_rand']=40;
//Kantelenen van de cirkel 0 to 90 °
$conf['kantel']=45;
//Titel vand de grafiek
$conf['titel']='Voorbeeld taartgrafiek';
//Kleur van lijnen en tekst, zwart is het best te gebruiken. In hex vormaat
//Meer over hex kleuren vind je hier http://html-color-codes.com/
$conf['Text-line-color']='#000000';
//kleuren
$colors=array('#FF0000','#00FF00','#0000FF');
//Input alles array(waarde, naam);
$input[]=array(367,'Firefox 1.0');
$input[]=array(734,'Internet Explorer 6.0');
$input[]=array(25,'Netscape 5.0');

/*------------------------------------*/
//Controle op input
/*------------------------------------*/
if(!is_numeric($conf['diameter'])){
$conf['diameter']=300;
$conf['alert']='Opgegeven diameter is geen nummer';
}
if(!is_numeric($conf['breedte_rand'])){
$conf['breedte_rand']=40;
$conf['alert']='Opgegeven rand is geen nummer';
}
elseif(!is_numeric($conf['kantel']) || $conf['kantel']<0 || $conf['kantel']>90){
$conf['kantel']=45;
$conf['alert']='Kan de figuur maar draaien van 0 tot 90 graden';
}
elseif(strlen(str_replace('#','',$conf['Text-line-color'])) != 6){
$conf['Text-line-color']='#000000';
$conf['alert']='Text-line-color is geen hex kleur';
}
elseif(count($input)>count($colors)){
exit('Je hebt te weinig kleuren opgegeven voor het aantal resultaten:<br>Kleuren: '.count($colors).'<br>Resultaten: '.count($input).'<br>Je mag dezelfde kleuren meer dan 1 keer opgeven in de array colors');
}
/*------------------------------------*/
//Bepaalde gegevens omzetten
/*------------------------------------*/
preg_match('/(#)*([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})/',$conf['Text-line-color'],$hex);
$conf['r']=hexdec($hex[2]);
$conf['g']=hexdec($hex[3]);
$conf['b']=hexdec($hex[4]);
//Sorteren invoer
sort($input, SORT_NUMERIC);
reset($input);
/*------------------------------------*/
//Input berekenen
/*------------------------------------*/
//Totaal berekenen
$results['total']=0;
for($a=0;$a<count($input);$a++){
$results['total']+=$input[$a][0];
}
//
for($a=0;$a<count($input);$a++){
//Hoeveel graden van de cirkel
$input[$a][2]=(($input[$a][0]/$results['total'])*360);
//Vorige aantal graden
if($a>0){
$previous=$input[$a-1][4];
}
else{
$previous=0;
}
//Beginnen cirkel deel
$input[$a][3]=$previous;
//Eindigen cirkel deel
$input[$a][4]=$input[$a][2]+$previous;
//Procent van 100
$input[$a][5]=round((100/$results['total'])*$input[$a][0],1);
}

/*------------------------------------*/
//CIRKEL TEKENEN
/*------------------------------------*/
//Hoofd circkel
$hoofd['Xas']=($conf['diameter']/2);
$hoofd['Arcw']=$conf['diameter'];
$hoofd['Arch']=$conf['diameter'];
$hoofd['Arcs']=0;
$hoofd['Arce']=360;
//Kantelen
$hoofd['Arch']=sin(deg2rad($conf['kantel'])) * $hoofd['Arcw'];
$hoofd['Yas']=($hoofd['Arch']/2)+20;
//Randje cirkel
if($conf['kantel']!=90){
$hoofd_rand['Xas']=$hoofd['Xas'];
$hoofd_rand['Yas']=($hoofd['Yas']-($hoofd['Arch']/2)) + ($hoofd['Arch']/2) + (sin(deg2rad(90 - $conf['kantel'])) * $conf['breedte_rand']);
$hoofd_rand['Arcw']=$hoofd['Arcw'];
$hoofd_rand['Arch']=$hoofd['Arch'];
$hoofd_rand['Arcs']=0;
$hoofd_rand['Arce']=180;
$hoofd_rand['Arch']=$hoofd['Arch'];
//Lijnen randje
$rand['Xb']=$hoofd['Xas']-($hoofd['Arcw']/2);
$rand['Yb']=$hoofd['Yas'];
$rand['Xe']=$rand['Xb'];
$rand['Ye']=(sin(deg2rad(90 - $conf['kantel'])) * $conf['breedte_rand']);
$rand['kantel']=$rand['Ye'];
$rand['Ye']+=$hoofd['Yas'];
}
else{
$rand['kantel']=0;
}
$text['height']=3;
$text['tlh']=3;
$text['dotx']=20+$hoofd['Arcw'];
/*------------------------------------*/
//Figuur tekenen
/*------------------------------------*/
/*------------------------------------*/
//Kleuren maken
/*------------------------------------*/
$img = imagecreate($conf['diameter']+$conf['legende'], ($hoofd['Yas']*2)+$rand['kantel']);
$bg = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, $conf['r'], $conf['g'], $conf['b']);
for($a=0;$a<count($input);$a++){
preg_match('/(#)*([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})/',$colors[$a],$hex);
$colors[$a] = imagecolorallocate($img, hexdec($hex[2]), hexdec($hex[3]), hexdec($hex[4]));
$color1=(hexdec($hex[2])>=100)?(hexdec($hex[2])-100):0;
$color2=(hexdec($hex[3])>=100)?(hexdec($hex[3])-100):0;
$color3=(hexdec($hex[4])>=100)?(hexdec($hex[4])-100):0;
$colors_d[$a] = imagecolorallocate($img, $color1, $color2, $color3);
} 
/*------------------------------------*/
//Titel zetten
/*------------------------------------*/
imagestring($img, $text['height'], $hoofd['Xas']-(strlen($conf['titel'])*$text['height']), 0, $conf['titel'], $black);
/*------------------------------------*/
//3D rand
/*------------------------------------*/
if($conf['kantel']!=90){
	for($a=0;$a<count($input);$a++){
		for($b=$rand['kantel'];$b>0;$b--){
			imagearc($img, $hoofd['Xas'], $hoofd['Yas']+$b, $hoofd['Arcw'], $hoofd['Arch'], $input[$a][3], $input[$a][4], $colors_d[$a]);
		}
	}
}
/*------------------------------------*/
//Hoofd delen van cirkel
/*------------------------------------*/
for($a=0;$a<count($input);$a++){
	imagefilledarc($img, $hoofd['Xas'], $hoofd['Yas'], $hoofd['Arcw'], $hoofd['Arch'], $input[$a][3], $input[$a][4], $colors[$a], IMG_ARC_PIE);
}
/*------------------------------------*/
//Zwart randje rond de cirkels
/*------------------------------------*/
if($conf['kantel']!=90){
	imagearc($img, $hoofd_rand['Xas'], $rand['Ye'], $hoofd_rand['Arcw'], $hoofd_rand['Arch'], $hoofd_rand['Arcs'], $hoofd_rand['Arce'], $black);
	imageline($img, $rand['Xb'], $rand['Yb'], $rand['Xe'], $rand['Ye'], $black);
	imageline($img, $rand['Xb']+$hoofd['Arcw'], $rand['Yb'], $rand['Xe']+$hoofd['Arcw'], $rand['Ye'], $black);
}
imagearc($img, $hoofd['Xas'], $hoofd['Yas'], $hoofd['Arcw'], $hoofd['Arch'], $hoofd['Arcs'], $hoofd['Arce'], $black);
/*------------------------------------*/
//Legende tekenen
/*------------------------------------*/
for($a=0;$a<count($input);$a++){
  imagefilledrectangle($img, $text['dotx'], ($a*20), $text['dotx']+10, ($a*20)+10, $colors[$a]);
  imagerectangle($img, $text['dotx'], ($a*20), $text['dotx']+10, ($a*20)+10, $black);
  imagestring($img, $text['height'], $text['dotx']+15, ($a*20), $input[$a][1].' ('.$input[$a][5].' %)', $black);
}
/*------------------------------------*/
//Alerts
/*------------------------------------*/
if(isset($conf['alert'])){
$red = imagecolorallocate($img, 255, 0, 0);
imagestring($img, $text['height'], 0, 9, 'Error: '.$conf['alert'], $red);
}
/*------------------------------------*/
//Figuur outputten
/*------------------------------------*/
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
?>