imagepng-porbleem
De volgende script werkt niet zoals het verwacht wordt.
De bedoeling is dat je de naam van een paar bedrijven en de hoeveelheid geld dat ieder bedrijf overgemaakt heeft aangeeft en het programma moet een grafiek maken van de naam van bedrijven op de horizontale lijn en de hoeveelheid van het geld op de verticale lijn.
Ik heb de script een paar keer doorgenomen maar niks gevonden waardoor het porgramma het niet normaal zou functioneren.
Zou je er a.u.b een kijkje op willen gooien en zeggen waar de fout is?
Alvast bedankt.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
header("Content-type: image/png");
class simpelbar{
private $margin=20; //The margin around of chart
private $marginverticalline=20; //place of vertical line
private $marginhorizentalline=40;//place of horizontal line
private $barmargin=10; //distance between two cells
private $inputwidth; //inserted width
private $inputheight; //inserted height
private $cells=array(); //array for the companies information
private $font="luxisri.ttf"; //font
function __construct($width,$height){
$this->inputwidth=$width;
$this->inputheight=$height;
}
function addbar($company,$money){
$this->cells[$company]=$money;
}
private function fontadjust($widthbar){
$fontsize=$widthbar;
foreach($this->cells as $key=>$val){
while(true){
$textsize=imageTTFbbox($fontsize,0,$this->font,$key);
if(abs($textsize[2])<$widthbar){
break;
}
$fontsize--;
}
}
return $fontsize;
}
function draw(){
$realwidth=$this->inputwidth-$this->margin*2; //the width of image
$realheight=$this->inputheight-$this->margin*2; //the height of image
$ystarthorizentalline=$realheight-$this->marginhorizentalline; // the begging y-point of horizontal line
$xendhorizentalline=$realwidth-$this->margin; //the end x-point of horizontal line
$max=max($this->cells); //the maximum amount of the money
$totalbars=count($this->cells); //total cells of the chart
$verticallinelong=$ystarthorizentalline-$this->margin; //the height of the vertical line
$widthbar=(int)(($realwidth-(2*$this->margin)-($this->barmargin*($totalbars+1)))/$totalbars); //the width of a cell
$text_size=$this->fontadjust($widthbar); //the text size
$image=imagecreate($realwidth,$realheight);
$red=imagecolorallocate($image,255,0,0);
$blue=imagecolorallocate($image,0,0,255);
$black=imagecolorallocate($image,0,0,0);
imageline($image, $this->margin,$ystarthorizentalline,$this->margin,$this->margin,$black);
imageline($image,$this->margin,$ystarthorizentalline,$xendhorizentalline,$ystarthorizentalline,$black);
$xbar=$this->margin+$this->barmargin; //the x-point of the first cell
foreach($this->cells as $key=>$val){
$ybar=$ystarthorizentalline-(int)($val/$max*$verticallinelong); // the y-point of first cell
$nextxbar=$xbar+$widthbar;// the second x-point of the cell
$nextybar=$this->marginhorizentalline;// the second y-point of the cell
imagefilledrectangle($image,$xbar,$ybar,$nextxbar,$nextybar,$blue);
$box=imageTTFbbox($text_size,0,$this->font,$key);
$xstartfont=$xbar+(int)(($widthbar-abs($box[2]))/2); //the begging of the text under the cells
imageTTFtext($image,35,0,$xstartfont,$realheight-5,$black,$this->font,$key);
$xbar=$nextxbar+$this->barmargin;//counting of the x-point of the next cell
}
imagepng($image);
}
}
$test=new simpelbar(400,300);
$test->addbar("Shell",300);
$test->addbar("Philips",200);
$test->addbar("CCC",400);
$test->draw();
?>
header("Content-type: image/png");
class simpelbar{
private $margin=20; //The margin around of chart
private $marginverticalline=20; //place of vertical line
private $marginhorizentalline=40;//place of horizontal line
private $barmargin=10; //distance between two cells
private $inputwidth; //inserted width
private $inputheight; //inserted height
private $cells=array(); //array for the companies information
private $font="luxisri.ttf"; //font
function __construct($width,$height){
$this->inputwidth=$width;
$this->inputheight=$height;
}
function addbar($company,$money){
$this->cells[$company]=$money;
}
private function fontadjust($widthbar){
$fontsize=$widthbar;
foreach($this->cells as $key=>$val){
while(true){
$textsize=imageTTFbbox($fontsize,0,$this->font,$key);
if(abs($textsize[2])<$widthbar){
break;
}
$fontsize--;
}
}
return $fontsize;
}
function draw(){
$realwidth=$this->inputwidth-$this->margin*2; //the width of image
$realheight=$this->inputheight-$this->margin*2; //the height of image
$ystarthorizentalline=$realheight-$this->marginhorizentalline; // the begging y-point of horizontal line
$xendhorizentalline=$realwidth-$this->margin; //the end x-point of horizontal line
$max=max($this->cells); //the maximum amount of the money
$totalbars=count($this->cells); //total cells of the chart
$verticallinelong=$ystarthorizentalline-$this->margin; //the height of the vertical line
$widthbar=(int)(($realwidth-(2*$this->margin)-($this->barmargin*($totalbars+1)))/$totalbars); //the width of a cell
$text_size=$this->fontadjust($widthbar); //the text size
$image=imagecreate($realwidth,$realheight);
$red=imagecolorallocate($image,255,0,0);
$blue=imagecolorallocate($image,0,0,255);
$black=imagecolorallocate($image,0,0,0);
imageline($image, $this->margin,$ystarthorizentalline,$this->margin,$this->margin,$black);
imageline($image,$this->margin,$ystarthorizentalline,$xendhorizentalline,$ystarthorizentalline,$black);
$xbar=$this->margin+$this->barmargin; //the x-point of the first cell
foreach($this->cells as $key=>$val){
$ybar=$ystarthorizentalline-(int)($val/$max*$verticallinelong); // the y-point of first cell
$nextxbar=$xbar+$widthbar;// the second x-point of the cell
$nextybar=$this->marginhorizentalline;// the second y-point of the cell
imagefilledrectangle($image,$xbar,$ybar,$nextxbar,$nextybar,$blue);
$box=imageTTFbbox($text_size,0,$this->font,$key);
$xstartfont=$xbar+(int)(($widthbar-abs($box[2]))/2); //the begging of the text under the cells
imageTTFtext($image,35,0,$xstartfont,$realheight-5,$black,$this->font,$key);
$xbar=$nextxbar+$this->barmargin;//counting of the x-point of the next cell
}
imagepng($image);
}
}
$test=new simpelbar(400,300);
$test->addbar("Shell",300);
$test->addbar("Philips",200);
$test->addbar("CCC",400);
$test->draw();
?>
Er zijn nog geen reacties op dit bericht.