Script for Fundraising Thermometer

Try the script.

therm.php (script that makes the image)

<?php
include ("imageSmoothArc_optimized.php"); // draw smooth circles
$secure = !empty($_GET['s']) && is_numeric($_GET['s']) ? $_GET['s'] : 0;
$target = !empty($_GET['t']) && is_numeric($_GET['t']) ? $_GET['t'] : 0;
$year = !empty($_GET['y']) && is_numeric($_GET['y']) ? $_GET['y'] : 0;
# user set variables STICK TO EVEN NUMBERS !!!!
$height 350//height of image
$width 100// width of image 
$bwidth 11// width of the bar - ODD VALUE to work withimageSmoothArc function
$bheight 200// height of the bar
$border 2// border width
$bulb 30// radius of bottom circle (without border)
$yoff 40// y-axis offset for distance from top of image
$imBorderCorner 20// image border radius
# create image & colours
$im = @imagecreatetruecolor($width$height) or die("Cannot Initialize new GD image stream");
imagealphablending($im,true);
$white imagecolorallocate($im255255255);
$whiteArray = array(2552552550);
$black imagecolorallocate($im000);
$blackArray = array(0000);
$red imagecolorallocate($im2553333);
$redArray = array(25533330);
$blue imagecolorallocate($im0150255);
$blueArray = array(01502550);
$lgrey imagecolorallocate($im127127127);
$lgreyArray = array(1271271270);
$dgrey imagecolorallocate($im118118118);
# generated variables
$percent $secure/$target;
$xoff $width/$bwidth/2// x-axis offset to find middle of image
$yearDisp intval($year-1)."/".intval($year-2000);
$pcText intval($secure/$target*100)."%";
$targetText "£".number_format(intval($target))."k";
$raisedText "£".number_format(intval($secure))."k";
// account for exceeding target
if($percent>1) {
    
$got intval(($percent-1) * $bheight); // size of bar for secured
    
$colGot $blue;
    
$colNotGot $red;
    
$colGotArray $blueArray;
    
$colNotGotArray $redArray;
}
else {
    
$got intval($percent $bheight); // size of bar for secured
    
$colGot $red;
    
$colNotGot $lgrey;
    
$colGotArray $redArray;
    
$colNotGotArray $lgreyArray;
}
# fill background
imagefill($im00$white);
# Image border
imageSmoothArc($im$imBorderCorner$imBorderCorner$imBorderCorner*2$imBorderCorner*2$blackArray9090); // top-left corner
imageSmoothArc($im$imBorderCorner$imBorderCorner$imBorderCorner*2-2$imBorderCorner*2-2$whiteArray9090); // top-left corner
imageSmoothArc($im$width-$imBorderCorner-1$imBorderCorner$imBorderCorner*2$imBorderCorner*2$blackArray00); // top-right corner
imageSmoothArc($im$width-$imBorderCorner-1$imBorderCorner$imBorderCorner*2-2$imBorderCorner*2-2$whiteArray00); // top-right corner
imageSmoothArc($im$imBorderCorner$imBorderCorner+$height-$imBorderCorner*2-1$imBorderCorner*2$imBorderCorner*2$blackArray180180); // bottom-left corner
imageSmoothArc($im$imBorderCorner$imBorderCorner+$height-$imBorderCorner*2-1$imBorderCorner*2-2$imBorderCorner*2-2$whiteArray180180); // bottom-left corner
imageSmoothArc($im$width-$imBorderCorner-1$imBorderCorner+$height-$imBorderCorner*2-1$imBorderCorner*2$imBorderCorner*2$blackArray270270); // bottom-right corner
imageSmoothArc($im$width-$imBorderCorner-1$imBorderCorner+$height-$imBorderCorner*2-1$imBorderCorner*2-2$imBorderCorner*2-2$whiteArray270270); // bottom-right corner
imageline($im0$imBorderCorner0$height $imBorderCorner$black); // left img border
imageline($im$width-1$imBorderCorner-5$width-1$height $imBorderCorner+3$black); // right img border
imageline($im$imBorderCorner-40$width $imBorderCorner+50$black); // top img border
imageline($im$imBorderCorner$height-1$width $imBorderCorner$height-1$black); // bottom img border
# draw bottom circle
imageSmoothArc($im$xoff+($bwidth/2), $yoff+$bheight+($bulb/2), $bulb+($border*2), $bulb+($border*2), $blackArray02*M_PI);
imageSmoothArc($im$xoff+($bwidth/2), $yoff+$bheight+($bulb/2), $bulb$bulb$colGotArray02*M_PI);
# draw top circle
$topcolor $got==200 $colGotArray $colNotGotArray;
imageSmoothArc($im$xoff+($bwidth/2)-1$yoff$bwidth+($border*2)-1$bwidth+($border*2), $blackArray02*M_PI);
imageSmoothArc($im$xoff+($bwidth/2)-1$yoff$bwidth-2$bwidth$topcolor02*M_PI);
# draw main bar
imagefilledrectangle($im$xoff-$border$yoff$xoff+$bwidth+$border$yoff+$bheight-$got$black);
imagefilledrectangle($im$xoff$yoff$xoff+$bwidth$yoff+$bheight-$got$colNotGot);
imagefilledrectangle($im$xoff-$border$yoff+$bheight-$got$xoff+$bwidth+$border$yoff+$bheight$black);
imagefilledrectangle($im$xoff$yoff+$bheight-$got$xoff+$bwidth$yoff+$bheight$colGot);
# draw markings at 10% intervals between 10% and 90%
for($i=1$i<10$i++) {imageline($im$xoff$yoff+$bheight-intval($bheight/10*$i), $xoff+($bwidth), $yoff+$bheight-intval($bheight/10*$i), $black);}
# draw markings at 25% intervals & add text
for($i=1$i<4$i++) {
    
$progress = array('','75%','50%','25%'); // blank array field for 100%
    
imageline($im$xoff+$bwidth+$border$yoff+$bheight-intval($bheight/4*$i), $xoff+($bwidth)+$border+4$yoff+$bheight-intval($bheight/4*$i), $black);
    
imagettftext($im8062, ($bheight/4*$i)+$yoff$black'fonts/FreeSans.ttf'$progress[$i]);
    }
# add text
function writeTextCenter($im$fontSize$fontFamily$text$yoff2$color$xoff$bwidth$yoff$bheight$bulb) {
    
$targetTextBox imagettfbbox($fontSize0"fonts/$fontFamily"$text);
    
$targetTextXoff $targetTextBox[0] + $targetTextBox[2] / 2;
    return 
imagettftext($im$fontSize0$xoff+($bwidth/2)-$targetTextXoff$yoff+$bheight+($bulb/2)+$yoff2$color"fonts/$fontFamily"$text);
}
writeTextCenter($im12'FreeSansBold.ttf'$yearDisp, ($yoff+$bheight+($bulb/2))*-22,$black$xoff$bwidth$yoff$bheight$bulb); // 18
writeTextCenter($im9'FreeSansBold.ttf',$pcText6$black$xoff$bwidth$yoff$bheight$bulb);
writeTextCenter($im9'FreeSans.ttf''Target'36$black$xoff$bwidth$yoff$bheight$bulb);
writeTextCenter($im10'FreeSansBold.ttf'$targetText52$black$xoff$bwidth$yoff$bheight$bulb);
writeTextCenter($im9'FreeSans.ttf''Raised'68$black$xoff$bwidth$yoff$bheight$bulb);
writeTextCenter($im10'FreeSansBold.ttf'$raisedText84$black$xoff$bwidth$yoff$bheight$bulb);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
?>

imageSmoothArc_optimized.php (function used in the image making script)

<?php

/*
    
    Copyright (c) 2006-2008 Ulrich Mierendorff

    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the "Software"),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.

*/

function imageSmoothArcDrawSegment (&$img$cx$cy$a$b$color$start$stop$seg)
{
    
// Originally written from scratch by Ulrich Mierendorff, 06/2006
    // Rewritten and improved, 04/2007, 07/2007
    // Optimized circle version: 03/2008
    
    // Please do not use THIS function directly. Scroll down to imageSmoothArc(...).
    
    
$fillColor imageColorExactAlpha$img$color[0], $color[1], $color[2], $color[3] );
    switch (
$seg)
    {
        case 
0$xp = +1$yp = -1$xa 1$ya = -1; break;
        case 
1$xp = -1$yp = -1$xa 0$ya = -1; break;
        case 
2$xp = -1$yp = +1$xa 0$ya 0; break;
        case 
3$xp = +1$yp = +1$xa 1$ya 0; break;
    }
    for ( 
$x 0$x <= $a$x += ) {
        
$y $b sqrt- ($x*$x)/($a*$a) );
        
$error $y - (int)($y);
        
$y = (int)($y);
        
$diffColor imageColorExactAlpha$img$color[0], $color[1], $color[2], 127-(127-$color[3])*$error );
        
imageSetPixel($img$cx+$xp*$x+$xa$cy+$yp*($y+1)+$ya$diffColor);
        
imageLine($img$cx+$xp*$x+$xa$cy+$yp*$y+$ya $cx+$xp*$x+$xa$cy+$ya$fillColor);
    }
    for ( 
$y 0$y $b$y += ) {
        
$x $a sqrt- ($y*$y)/($b*$b) );
        
$error $x - (int)($x);
        
$x = (int)($x);
        
$diffColor imageColorExactAlpha$img$color[0], $color[1], $color[2], 127-(127-$color[3])*$error );
        
imageSetPixel($img$cx+$xp*($x+1)+$xa$cy+$yp*$y+$ya$diffColor);
    }
}


function 
imageSmoothArc ( &$img$cx$cy$w$h$color$start$stop)
{
    
// Originally written from scratch by Ulrich Mierendorff, 06/2006
    // Rewritten and improved, 04/2007, 07/2007
    // Optimized circle version: 03/2008
    // compared to old version:
    // + Support for transparency added
    // + Improved quality of edges & antialiasing
    
    // note: This function does not represent the fastest way to draw elliptical
    // arcs. It was written without reading any papers on that subject. Better
    // algorithms may be twice as fast or even more.
    
    // Parameters:
    // $cx      - Center of ellipse, X-coord
    // $cy      - Center of ellipse, Y-coord
    // $w       - Width of ellipse ($w >= 2)
    // $h       - Height of ellipse ($h >= 2 )
    // $color   - Color of ellipse as a four component array with RGBA
    // $start   - Starting angle of the arc: 0, PI/2, PI, PI/2*3, 2*PI,... (0,90°,180°,270°,360°,...)
    // $stop    - Stop     angle of the arc: 0, PI/2, PI, PI/2*3, 2*PI,... (0,90°,180°,270°,360°,...)
    // $start _can_ be greater than $stop!
    // If any value is not in the given range, results are undefined!
    
    // This script does not use any special algorithms, everything is completely
    // written from scratch; see http://de.wikipedia.org/wiki/Ellipse for formulas.
    
    
while ($start 0)
        
$start += 2*M_PI;
    while (
$stop 0)
        
$stop += 2*M_PI;
    
    while (
$start 2*M_PI)
        
$start -= 2*M_PI;
    
    while (
$stop 2*M_PI)
        
$stop -= 2*M_PI;
    
    
    if (
$start $stop)
    {
        
imageSmoothArc ( &$img$cx$cy$w$h$color$start2*M_PI);
        
imageSmoothArc ( &$img$cx$cy$w$h$color0$stop);
        return;
    }
    
    
$a 1.0*round ($w/2);
    
$b 1.0*round ($h/2);
    
$cx 1.0*round ($cx);
    
$cy 1.0*round ($cy);
    
    for (
$i=0$i<4;$i++)
    {
        if (
$start < ($i+1)*M_PI/2)
        {
            if (
$start $i*M_PI/2)
            {
                if (
$stop > ($i+1)*M_PI/2)
                {
                    
imageSmoothArcDrawSegment($img$cx$cy$a$b$color$start, ($i+1)*M_PI/2$i);
                }
                else
                {
                    
imageSmoothArcDrawSegment($img$cx$cy$a$b$color$start$stop$i);
                    break;
                }
            }
            else
            {
                if (
$stop > ($i+1)*M_PI/2)
                {
                    
imageSmoothArcDrawSegment($img$cx$cy$a$b$color$i*M_PI/2, ($i+1)*M_PI/2$i);
                }
                else
                {
                    
imageSmoothArcDrawSegment($img$cx$cy$a$b$color$i*M_PI/2$stop$i);
                    break;
                }
            }
        }
    }
}
?>