imagegraph
// Functie
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
function imagegraph(&$img, $title, $mode, $data) {
if (!$img || ($mode != 1 && $mode != 2) || count($data) == 0) return false;
$x = imagesx($img);
$y = imagesy($img);
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
$grey = imagecolorallocate($img, 210, 210, 210);
$darkgrey = imagecolorallocate($img, 170, 170, 170);
$colors = array();
$colors[] = imagecolorallocate($img, 255, 0, 0);
$colors[] = imagecolorallocate($img, 0, 255, 0);
$colors[] = imagecolorallocate($img, 0, 0, 255);
$colors[] = imagecolorallocate($img, 255, 255, 0);
$colors[] = imagecolorallocate($img, 255, 0, 255);
$colors[] = imagecolorallocate($img, 0, 255, 255);
imagefill($img, 0, 0, $white);
imagerectangle($img, 0, 0, $x-1, $y-1, $black); //border
imagefilledrectangle($img, 30, 15, $x-15, $y-30, $grey); //achtergrond
imageline($img, 30, 15, 30, $y-30, $black); //y as
imageline($img, 30, $y-30, $x-15, $y-30, $black); //x as
$fy = imagefontheight(1);
$fx = imagefontwidth(1);
$rasterx = 45;
while ($rasterx <= $x-15) { //x raster
imageline($img, $rasterx, $y-31, $rasterx, 15, $darkgrey);
$rasterx += 15;
}
$rastery = $y-45;
while ($rastery >= 15) { //y raster
imageline($img, 31, $rastery, $x-15, $rastery, $darkgrey);
$rastery -= 15;
}
$graph = 0;
while($graph < count($data)) {
$datax = array_keys($data[$graph]);
$datay = array_values($data[$graph]);
if (!isset($largestx) || max($datax) > $largestx) $largestx = max($datax);
if (!isset($smallestx) || min($datax) < $smallestx) $smallestx = min($datax);
if (!isset($largesty) || max($datay) > $largesty) $largesty = max($datay);
if (!isset($smallesty) || min($datay) < $smallesty) $smallesty = min($datay);
$graph++;
}
$numrastx = ($x-45)/30;
$incx = ($largestx-$smallestx)/$numrastx;
$numrasty = ($y-45)/30;
$incy = ($largesty-$smallesty)/$numrasty;
$txtlocx = 30;
$i = 0;
while ($txtlocx <= $x-15) { //x as strings
imagestringup($img, 1, round($txtlocx-($fy/2)), $y-30+strlen(round($i*$incx+$smallestx, 0))*$fx, round($i*$incx+$smallestx, 0), $black);
$i++;
$txtlocx += 30;
}
$txtlocy = $y-30;
$i = 0;
while ($txtlocy >= 15) { //y as strings
imagestring($img, 1, 30-strlen(round($i*$incy+$smallesty, 0))*$fx, $txtlocy-round($fy/2), round($i*$incy+$smallesty, 0), $black);
$i++;
$txtlocy -= 30;
}
$graph = 0;
while ($graph < count($data)) {
if (!isset($colors[$graph])) break; //kleuren op :(
ksort($data[$graph]);
$datax = array_keys($data[$graph]);
$datay = array_values($data[$graph]);
$i = 1;
while ($i < count($datax)) {
if ($mode == 1) { //lijn voor graph
$dotx = ((($x-45)/($largestx-$smallestx))*($datax[$i-1]-$smallestx))+30;
$dotx2 = ((($x-45)/($largestx-$smallestx))*($datax[$i]-$smallestx))+30;
if ($largesty != $smallesty) {
$doty = $y-30-((($y-45)/($largesty-$smallesty))*($datay[$i-1]-$smallesty));
$doty2 = $y-30-((($y-45)/($largesty-$smallesty))*($datay[$i]-$smallesty));
} else {
$doty = $y-30-(($y-45)/2);
$doty2 = $y-30-(($y-45)/2);
}
imageline($img, $dotx, $doty, $dotx2, $doty2, $colors[$graph]);
} elseif ($mode == 2) { //dots voor graph
$dotx = ((($x-45)/($largestx-$smallestx))*($datax[$i]-$smallestx))+30;
if ($largesty != $smallesty) $doty = $y-30-((($y-45)/($largesty-$smallesty))*($datay[$i]-$smallesty));
else $doty = $y-30-(($y-45)/2);
imagefilledellipse($img, $dotx, $doty, 3, 3, $colors[$graph]);
}
$i++;
}
$graph++;
}
imagestring($img, 2, 30, 1, $title, $black);
return true;
}
?>
function imagegraph(&$img, $title, $mode, $data) {
if (!$img || ($mode != 1 && $mode != 2) || count($data) == 0) return false;
$x = imagesx($img);
$y = imagesy($img);
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
$grey = imagecolorallocate($img, 210, 210, 210);
$darkgrey = imagecolorallocate($img, 170, 170, 170);
$colors = array();
$colors[] = imagecolorallocate($img, 255, 0, 0);
$colors[] = imagecolorallocate($img, 0, 255, 0);
$colors[] = imagecolorallocate($img, 0, 0, 255);
$colors[] = imagecolorallocate($img, 255, 255, 0);
$colors[] = imagecolorallocate($img, 255, 0, 255);
$colors[] = imagecolorallocate($img, 0, 255, 255);
imagefill($img, 0, 0, $white);
imagerectangle($img, 0, 0, $x-1, $y-1, $black); //border
imagefilledrectangle($img, 30, 15, $x-15, $y-30, $grey); //achtergrond
imageline($img, 30, 15, 30, $y-30, $black); //y as
imageline($img, 30, $y-30, $x-15, $y-30, $black); //x as
$fy = imagefontheight(1);
$fx = imagefontwidth(1);
$rasterx = 45;
while ($rasterx <= $x-15) { //x raster
imageline($img, $rasterx, $y-31, $rasterx, 15, $darkgrey);
$rasterx += 15;
}
$rastery = $y-45;
while ($rastery >= 15) { //y raster
imageline($img, 31, $rastery, $x-15, $rastery, $darkgrey);
$rastery -= 15;
}
$graph = 0;
while($graph < count($data)) {
$datax = array_keys($data[$graph]);
$datay = array_values($data[$graph]);
if (!isset($largestx) || max($datax) > $largestx) $largestx = max($datax);
if (!isset($smallestx) || min($datax) < $smallestx) $smallestx = min($datax);
if (!isset($largesty) || max($datay) > $largesty) $largesty = max($datay);
if (!isset($smallesty) || min($datay) < $smallesty) $smallesty = min($datay);
$graph++;
}
$numrastx = ($x-45)/30;
$incx = ($largestx-$smallestx)/$numrastx;
$numrasty = ($y-45)/30;
$incy = ($largesty-$smallesty)/$numrasty;
$txtlocx = 30;
$i = 0;
while ($txtlocx <= $x-15) { //x as strings
imagestringup($img, 1, round($txtlocx-($fy/2)), $y-30+strlen(round($i*$incx+$smallestx, 0))*$fx, round($i*$incx+$smallestx, 0), $black);
$i++;
$txtlocx += 30;
}
$txtlocy = $y-30;
$i = 0;
while ($txtlocy >= 15) { //y as strings
imagestring($img, 1, 30-strlen(round($i*$incy+$smallesty, 0))*$fx, $txtlocy-round($fy/2), round($i*$incy+$smallesty, 0), $black);
$i++;
$txtlocy -= 30;
}
$graph = 0;
while ($graph < count($data)) {
if (!isset($colors[$graph])) break; //kleuren op :(
ksort($data[$graph]);
$datax = array_keys($data[$graph]);
$datay = array_values($data[$graph]);
$i = 1;
while ($i < count($datax)) {
if ($mode == 1) { //lijn voor graph
$dotx = ((($x-45)/($largestx-$smallestx))*($datax[$i-1]-$smallestx))+30;
$dotx2 = ((($x-45)/($largestx-$smallestx))*($datax[$i]-$smallestx))+30;
if ($largesty != $smallesty) {
$doty = $y-30-((($y-45)/($largesty-$smallesty))*($datay[$i-1]-$smallesty));
$doty2 = $y-30-((($y-45)/($largesty-$smallesty))*($datay[$i]-$smallesty));
} else {
$doty = $y-30-(($y-45)/2);
$doty2 = $y-30-(($y-45)/2);
}
imageline($img, $dotx, $doty, $dotx2, $doty2, $colors[$graph]);
} elseif ($mode == 2) { //dots voor graph
$dotx = ((($x-45)/($largestx-$smallestx))*($datax[$i]-$smallestx))+30;
if ($largesty != $smallesty) $doty = $y-30-((($y-45)/($largesty-$smallesty))*($datay[$i]-$smallesty));
else $doty = $y-30-(($y-45)/2);
imagefilledellipse($img, $dotx, $doty, 3, 3, $colors[$graph]);
}
$i++;
}
$graph++;
}
imagestring($img, 2, 30, 1, $title, $black);
return true;
}
?>
//Testcode voor voorbeeld
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
$data = array();
$i = -50;
while ($i <= 50) {
$data[0][$i] = sin(0.1*$i)*50;
$data[1][$i] = cos(0.1*$i)*50;
$data[2][$i] = sin(0.1*$i+pi())*50;
$data[3][$i] = cos(0.1*$i+pi())*50;
$data[4][$i] = $i;
$data[5][$i] = -$i;
$i++;
}
$img = imagecreatefromjpeg('/home/jorrizza/pics/boosmannetje.jpg');
$img2 = imagecreate(imagesx($img), imagesy($img));
imagegraph($img2, 'Testgrafiek', 1, $data);
imagecopymerge($img, $img2, 0, 0, 0, 0, imagesx($img2), imagesy($img2), 75);
header('Content-Type: image/png');
imagepng($img);
?>
$data = array();
$i = -50;
while ($i <= 50) {
$data[0][$i] = sin(0.1*$i)*50;
$data[1][$i] = cos(0.1*$i)*50;
$data[2][$i] = sin(0.1*$i+pi())*50;
$data[3][$i] = cos(0.1*$i+pi())*50;
$data[4][$i] = $i;
$data[5][$i] = -$i;
$i++;
}
$img = imagecreatefromjpeg('/home/jorrizza/pics/boosmannetje.jpg');
$img2 = imagecreate(imagesx($img), imagesy($img));
imagegraph($img2, 'Testgrafiek', 1, $data);
imagecopymerge($img, $img2, 0, 0, 0, 0, imagesx($img2), imagesy($img2), 75);
header('Content-Type: image/png');
imagepng($img);
?>