PHP Bargraph Generator

From Codevault

Jump to: navigation, search

This script is loaded using an image tag and generates data based on information added as GET parameters.

For example: <img src="bar.php?value=100&max=256&info" /> Generates File:bar.png

This script requires at least four files to use. The first is an HTML file which loads the image (duh), the second is the background "bar.png" image which the graph is built on, the third is a ttf font file, and the fourth is the bar.php script which does all the work. You can see the latter on this page, or you can click the link below to download all the files.

Download: [1]

Advanced Preview: [2]

Renowned Media Blog Entry: [3]

bar.php


<?php
$image_file "bar.png";
$value $_GET['value'] + 0# we use values 0 to 100
if ($value 0) {
    $value 0;
} else if ($value 100) {
    $value 100;
}
if ((isset($_GET['value'])) && (isset($_GET['max'])) && ($_GET['max'] > 0)) {
    $value = ($_GET['value'] / $_GET['max'] + 0) * 100;
}
if (isset($_GET['info'])) {
    $imx imagecreatefrompng ($image_file);
    $image_height imagesy($imx) + 14;
    $image_width imagesx($imx);
    
    $im imagecreatetruecolor($image_width$image_height);
    imagesavealpha($imtrue);
    $trans_colour imagecolorallocatealpha($im000127);
    imagefill($im00$trans_colour);
    
    imagecopy($im$imx0000$image_widthimagesy($imx));
    imagedestroy($imx);
    
    $font_family "calibri.ttf";
    $font_size 10;
    $black imagecolorallocate($im000);
    if (isset($_GET['max']))
        $string $_GET['value'] . "/" . ($_GET['max'] + 0) . " (" round($value) . "%)";
    else
        $string $value "%";
    $box = @imageTTFBbox($font_size,0,$font_family,$string);
    $b_width abs($box[4] - $box[0]);
    $b_height abs($box[5] - $box[1]);
    imagettftext($im$font_size0$image_width/2-$b_width/218$black$font_family$string);
} else {
    $im imagecreatefrompng ($image_file); # we load the background file (faster than drawing it)
}
putenv('GDFONTPATH=' realpath('./'));
header ("Content-type: image/png"); # we are making a PNG but you can change to a GIF or JPEG


$bar_distance 14;    # the distance from the start of the first bar to the start of the second bar
$bar_width 8;        # the width of a bar rectangle
$bar_height 3;    # the height of a bar
$bar_y 2;            # how far a bar is from the top of the image
$bar_x 2;            # how far the offset is on our first bar
$colors = array(    # our 10 colors from left to right in RGB decimal pairs
            array(771801),
            array(1151801),
            array(1251961),
            array(1472100),
            array(17822612),
            array(2281930),
            array(2551800),
            array(2551380),
            array(238962),
            array(213400)
        );
$value /= 10;        # divide the value by 10 and round the result
$value round($value);
for ($i 0$i $value$i++) {
    $color imagecolorallocate($im$colors[$i][0], $colors[$i][1], $colors[$i][2]); # only allocate color if we're using it
    imagefilledrectangle($im$bar_distance $i $bar_x$bar_y$bar_distance $i $bar_x $bar_width 1$bar_y $bar_height 1$color);    # draw the bars
}
imagepng($im);        # output the image
?>

Personal tools