-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.php
55 lines (46 loc) · 1.91 KB
/
image.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
<?php
# evaluate GET parameters
if (isset($_GET['votes']) && isset($_GET['max']) && isset($_GET['low']) && isset($_GET['high']) && isset($_GET['day'])) {
$votes = $_GET['votes'];
$max = $_GET['max'];
$low = $_GET['low'];
$high = $_GET['high'];
$letter = substr($_GET['day'],0,1);
}
if (empty($max)) die();
# how big a step in the 0-127 alpha range will be used to represent one vote?
# only take the range from lowest vote to highest vote into account for greater visual effect
$alphastep = 127-127/($high-$low);
# number of votes (see above) = number of steps used
$steps = $votes-$low;
# for text output, base percentages on the actual maximum number of votes
$votepercent = round($votes * (100/$max));
$votepercent = $votepercent . "%";
# create a new image, define colors and fonts to be used in it
$img = imagecreatetruecolor(100,50);
$green = imagecolorallocatealpha($img,0,200,0,$alphastep);
$white = imagecolorallocate($img,255,255,255);
$gray = imagecolorallocate($img,230,230,230);
$font = '/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf';
# enable alpha blending and fill with white background
imagealphablending($img, true);
imagefill($img, 0, 0, $white);
# layer the appropriate number of partly-transparent colored layers on top of each other
if ($steps > 0) {
for ($i=0; $i < $steps; $i++) {
imagefilledrectangle($img, 0, 0, 100, 50, $green);
}
}
# add text output, in a different color if number of colored layers was 0
if ($steps > 0) {
imagettftext($img, 20, 0, 38, 35, $white, $font, "$letter");
imagettftext($img, 10, 0, 05, 45, $white, $font, "$votepercent");
} else {
imagettftext($img, 20, 0, 38, 35, $gray, $font, "$letter");
imagettftext($img, 10, 0, 05, 45, $gray, $font, "$votepercent");
}
# send HTTP header, the image, and clean up after ourselves
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
?>