#!/usr/bin/perl
# Beispiel für einen grafischen Besucherzaehler 
# (sowas braucht eigentlich niemand)
use strict;
use warnings;
use GD;

$| = 1;

#--- Konfiguration ----------------
# Die Counter-Datei
my $COUNTER = "/tmp/count";
#----------------------------------

# aktuelle Zaehler
my $num = 0;
if (open IN_FILE, "<$COUNTER")
  {
  $num = <IN_FILE>;
  chomp($num);
  close(IN_FILE);
  }

# Image-Header
print "Content-type: image/png\n\n";

# nun den Zaehler mittels GD generieren
my $font = gdGiantFont;
my $char_x = $font->width;
my $char_y = $font->height;
# Bildgroesse hangt vom Font ab
my $picture_x = (1 + $char_x) * length($num) + 1;
my $picture_y = (1 + $char_y);
# Das Bild rot auf transparent
my $image = new GD::Image($picture_x, $picture_y);
my $background = $image->colorAllocate(0,0,0);
my $red = $image->colorAllocate(255,0,0);
$image->transparent($background);
$image->string($font, 0, 0, $num ,$red);
# und ausgeben
print $image->png;

# zum Schluss Zaehler erhoehen und speichern
$num++;
if (open OUT_FILE, ">$COUNTER")
  {
  flock(IN_File, 1);  # Zugriff (schreiben) sperren
  print OUT_FILE $num;
  flock(IN_File, 8);  # Zugriff (schreiben) freigeben
  }
close OUT_FILE;