Bitte warten...

PHP: Grafik-Beispiel

Hier ein einfaches Beispiel für die Möglichkeiten, mit PHP Grafiken zu erzeugen:

Code kopieren
<?php
  function rgb($image, $hex) {
    return imagecolorallocate($image, hexdec(substr($hex, 0, 2)), hexdec(substr($hex, 2, 2)), hexdec(substr($hex, 4, 2)));
  }
  
  $ua_color = array("cccccc","7755ff","5533ff","3311dd","dd0000","bb0000","ff7700","ee5500");
  
  $ua_logs = array(); $sum = array(); $top = 0;
  for ($i = 0; $i < 31; $i++) {
    for ($j = 0; $j < 8; $j++) {
      $ua_logs[$i][$j] = rand(0, 1000);
      $sum[$i] += $ua_logs[$i][$j];
      if ($sum[$i] > $top) $top = $sum[$i];
    }
  }
  $decs = pow(10, strlen($top)-1);
  $top = ceil($top/$decs)*$decs;
  if ($top < 10) $top = 10;
  $decs = pow(10, strlen($top)-1);
  $max = 2*substr($top, 0, 1); $step = floor(500/$max);
  
  $width = 700; $height = 580; $left = 60;
  $image = @imagecreatetruecolor($width, $height) or die("Cannot initialize new GD image stream!");
  imagefill($image, 0, 0, rgb($image, "004400"));
  
  imagesetthickness($image, 1); $bar_width = 20;
  for ($i = 0; $i < 32; $i++) {
    imageline($image, $left+$i*$bar_width, $height-50, $left+$i*$bar_width, $height-43, rgb($image, "007700"));
    if ($i != 31) {
      imagestring($image, 2, $left+6+$i*$bar_width, $height-40, str_pad($i+1, 2, "0", STR_PAD_LEFT), rgb($image, "ccee00"));
      $bottom = $height-51;
      for ($j = 0; $j < count($ua_logs[$i]); $j++) {
        $bar_height = $ua_logs[$i][$j]/$decs*2*$step;
        imagefilledrectangle($image, $left+1+$i*$bar_width, $bottom-$bar_height+1, $left-1+$bar_width+$i*$bar_width, $bottom, rgb($image, $ua_color[$j]));
        $bottom -= $bar_height;
      }
    }
  }

  for ($i = 0; $i <= $max; $i++) {
    imageline($image, $left-6, $height-50-$i*$step, $width-10, $height-50-$i*$step, rgb($image, "007700"));
    imagestring($image, 2, $left-10-strlen($i*$decs/2)*6, $height-58-$i*$step, $i*$decs/2, rgb($image, "ccee00"));
  }

  imagesetthickness($image, 2);
  imageline($image, $left, $height-49, $width-10, $height-49, rgb($image, "00cc00"));
  imageline($image, $left, 4, $left, $height-49, rgb($image, "00cc00"));
  
  header("content-type: image/png");
  imagepng($image);
  imagedestroy($image);
?>

Der Code erzeugt folgende Grafik:

PHP Grafik-Beispiel