Manchmal ist es vorteilhaft, graphische Buttons automatisch mittels PHP zu generieren. Im Beispiel werden die Bilder als Dateien abgelegt, damit Sie nicht bei jedem Aufruf erneut erzeugt werden müssen.
< ?php
// Schriftart
$font = "pfad_zur_schrift/Schriftart.ttf";
// Text
$text = htmlspecialchars($_GET['text']);
// Schriftgroesse
$font_size = htmlspecialchars($_GET['size']);
// Status
$status = htmlspecialchars($_REQUEST['status']);
if(!$text) {
// Standardtext
$text = "Das ist ein Button";
}
if(!$font_size) {
$font_size = 11;
}
// Dateiname des Bildes
$filename = md5($text."-".$font_size."-".$status).".gif";
// Pfad zu einem Ordner in dem das erzeugte Bild abgelegt wird
$tmp_path = "system/html/";
header("Content-type: image/gif");
if(!file_exists($tmp_path.$filename)) {
$box = imagettfbbox($font_size,0,$font,$text);
// Hoehe des Buttons wird auf Wunsch auch errechnet
// $im = imagecreate ($box[2]-$box[0]+5, abs($box[5]-$box[3])+5);
$im = imagecreate ($box[2]-$box[0]+5, 18);
$bg = ImageColorAllocate ($im, 255, 255, 255);
// Schriftfarbe grau
$color = ImageColorAllocate ($im, 93, 96, 98);
// beim Aktivstatus Schriftfarbe Schwarz
if($status == 'active')
{
$color = ImageColorAllocate ($im, 0, 0, 0);
}
ImageTTFText ($im, $font_size, 0, 0, 14, $color, $font, $text);
ImageGif ($im,$tmp_path.$filename);
ImageDestroy ($im);
}
readfile($tmp_path.$filename);
?>
Der Aufruf im HTML Quelltext lautet dann wie folgt:





