
:: Anbieterverzeichnis ::
Globale Branchen
Informieren Sie sich über ausgewählte
Unternehmen im
Anbieterverzeichnis von SELFPHP
:: SELFPHP Forum ::
Fragen rund um die Themen PHP? In über
120.000 Beiträgen finden Sie sicher die passende
Antwort! 
:: Newsletter ::
Abonnieren Sie hier den kostenlosen
SELFPHP Newsletter!
:: Qozido ::
Die Bilderverwaltung mit Logbuch für
Taucher und Schnorchler. 
|
|
Grafische Fortschrittsanzeige mit PHP erstellen  |
Beispielaufgabe
Nachfolgend stellen wir Ihnen eine Funktion vor, mit der Sie grafische Fortschrittsanzeigen komfortabel erstellen können. Gerade wenn es darum geht eine Fortschrittsanzeige auf mehreren Seiten (z.B. bei einer Umfrage) anzuzeigen, kann diese Fortschrittsanzeige visuell dem User anzeigen, wieviel Prozent der Umfrage bereits beantwortet wurde.
Die Funktion ist so konzipiert, dass Sie neben der Breite und Höhe für die Fortschrittsanzeige auch noch folgende Parameter einstellen können:
- die Rahmenfarbe der Grafik
- die Farbe der Aufteilungslininien - alle 10% wird eine gestrichelte vertikale Linie angezeigt
- die Hintergrundfarbe
- eine True-Type Schrift - hier muss der Pfad zu der TTF-Datei angegeben werden
- die Schriftgröße
Nachfolgend sehen Sie so eine generierte Fortschrittsanzeige:
progressbar-test.php
Nachfolgend sehen Sie den Zugriff auf die Funktion.
<img src="progressbar.php?progress=7"/>
|
progressbar.php
Hier sehen Sie noch die vollständige Funktion für die Erzeugung der Fortschrittsanzeige.
<?php
/**
* Fortschrittsanzeige
*
* @author SELFPHP OHG
* @copyright 2009, SELFPHP OHG
* @license BSD License
* @link http://www.selfphp.de
*
*/
function hexdecColor( $color ){
$colorRGB['r'] = hexdec( substr( $color, 0, 2 ) );
$colorRGB['g'] = hexdec( substr( $color, 2, 2 ) );
$colorRGB['b'] = hexdec( substr( $color, 4, 2 ) );
return $colorRGB;
}
function createProgressBar($environment, $step = 0){
if( $step < 0 || $step > 10 )
$step = 0;
$lineWidth = $environment['width'] / 10;
$progress = $environment['width'] - ( $step * $lineWidth );
$status = $step * 10;
$im = imagecreatetruecolor($environment['width'], $environment['height']);
$color = hexdecColor($environment['backgroundColor']);
$imBackgroundColor = imagecolorallocate($im, $color['r'], $color['g'], $color['b']);
$color = hexdecColor($environment['borderColor']);
$imBorderColor = imagecolorallocate($im, $color['r'], $color['g'], $color['b']);
// Set the background to be white
imagefilledrectangle($im, 0, 0, $environment['width'], $environment['height'], $imBackgroundColor);
// Set the line thickness to 5
imagesetthickness($im, 1);
// Draw the rectangle
imagerectangle($im, 0, 0, $environment['width'] - 1, $environment['height'] - 1, $imBorderColor);
$color = hexdecColor($environment['lineColor']);
$imLineColor = imagecolorallocate($im, $color['r'], $color['g'], $color['b']);
imagefilledrectangle($im, 0, 3, $environment['width'] - $progress, $environment['height'] - 4, $imBorderColor);
for ($i = 1; $i < 10; $i++ ){
imagedashedline ( $im , $lineWidth * $i , 2 , $lineWidth * $i , 60 , $imLineColor );
}
$text = $status . '%';
// Benötigte Schriftgröße kalkulieren
$fontsize = imageftbbox($environment['fontsize'], 0, $environment['font'], $text);
$x = $fontsize[0] + (imagesx($im) / 2) - ($fontsize[4] / 2) - 5;
$y = $fontsize[1] + (imagesy($im) / 2) - ($fontsize[5] / 2) - 5;
$black = imagecolorallocate($im, 0, 0, 0);
imagettftext($im, $environment['fontsize'], 0, $x, 18, $black, $environment['font'], $text);
// Output image to the browser
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
$environment['width'] = 326;
$environment['height'] = 25;
$environment['lineColor'] = "ffd0b2";
$environment['borderColor'] = "fe934f";
$environment['backgroundColor'] = "fff3eb";
$environment['font'] = 'bgothl.ttf';
$environment['fontsize'] = 14;
if (isset($_GET['progress']))
createProgressBar($environment, $_GET['progress']);
?>
|
Download
progessbar.zip

|
|
|
|
|


|