~josebagar/bicicleta/trunk

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
<?php
/*******************************************************************************
  Bicicleta
  Copyright 2008 Joseba GarcĂ­a Etxebarria
  
  Licensed under The MIT License
  Redistributions of files must retain the above copyright notice.
  
  Gets in charge of displaying a bar graphic with the information passed as
  a GET argument. Also, the arithmetic mean of all the values is computed
  and displayed in the background.
  Graphs are cached.
  Several parameters can be changed in the includes/config.php file.
  
  Be advised that I'll change to a better system than base64_encoding sooner
  than latter.
*******************************************************************************/


  require_once("includes/config.php");

  # Define values
  $height = 100 * $pixelheight + $border;

  # Get the values passed in the data array
  $data = unserialize(base64_decode($_GET["data"]));
  if(! is_array($data)) {
    return FALSE;
  }
  $n = count($data, 0);
  
  # Check if the image is cached
  # We md5sum it so we don't get a too long filename
  $graph_id = md5($_GET['data']);
  $location = $graph_cache.$graph_id;
  if(! file_exists($location)) {
    $values = array(); $legend = array();
    for($i = 0; $i < $n; $i++) {
      $values[] = $data[$i]["value"];
      $legends[] = $data[$i]["text"];
    }
    $max = max($values);
    if($n <= 0 || $max <= 0) {    # UPS!
      return FALSE;
    }
    $mean = array_sum($values) * 100. * $pixelheight / ($n * $max) ;
    $width = ($n+1)*$border + $n*$barwidth;
    
    $img = imagecreatetruecolor($width, $height);
    $bordercolor = imagecolorallocate($img, 150, 150, 150);
    $meancolor = imagecolorallocate($img, 225, 237, 255);
    $bgcolor = imagecolorallocate($img, 255, 255, 255);
    $barcolor = imagecolorallocate($img, 150, 192, 255);
    $textcolor = imagecolorallocate($img, 155, 155, 155);
    # Draw the white background and the outer frame
    imagefilledrectangle($img, 0, 0, $width-1, $height-1,
                         $bgcolor);                           # White background
    imagefilledrectangle($img, 0, $height-$mean, $width-1,
                         $height-1, $meancolor);                  # Display mean
    imagerectangle($img, 0, 0, $width-1, $height-1, $bordercolor);       # Frame
    
    # Draw the bars
    $x = $border;
    for($i=0; $i<$n; $i++) {
      imagefilledrectangle($img, $x, $height-($values[$i]*100.*$pixelheight/$max),
          $x + $barwidth, $height - 2, $barcolor);
      imagefttext($img, $bargraph_fontsize, 90., $x+$barwidth/2+$bargraph_fontsize/2, $height-$border, $textcolor,
          "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf", $values[$i]."Km. on ".$legends[$i]);
      $x += $barwidth + $border;
    }
    
    # Cache image and display it
    imagepng($img, $location);
    imagedestroy($img);
  }
  
  header("Content-type: image/png");
  readfile($location);
?>