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($im, 255, 255, 255);
$whiteArray = array(255, 255, 255, 0);
$black = imagecolorallocate($im, 0, 0, 0);
$blackArray = array(0, 0, 0, 0);
$red = imagecolorallocate($im, 255, 33, 33);
$redArray = array(255, 33, 33, 0);
$blue = imagecolorallocate($im, 0, 150, 255);
$blueArray = array(0, 150, 255, 0);
$lgrey = imagecolorallocate($im, 127, 127, 127);
$lgreyArray = array(127, 127, 127, 0);
$dgrey = imagecolorallocate($im, 118, 118, 118);
# generated variables
$percent = $secure/$target;
$xoff = $width/2 - $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($im, 0, 0, $white);
# Image border
imageSmoothArc($im, $imBorderCorner, $imBorderCorner, $imBorderCorner*2, $imBorderCorner*2, $blackArray, 90, 90); // top-left corner
imageSmoothArc($im, $imBorderCorner, $imBorderCorner, $imBorderCorner*2-2, $imBorderCorner*2-2, $whiteArray, 90, 90); // top-left corner
imageSmoothArc($im, $width-$imBorderCorner-1, $imBorderCorner, $imBorderCorner*2, $imBorderCorner*2, $blackArray, 0, 0); // top-right corner
imageSmoothArc($im, $width-$imBorderCorner-1, $imBorderCorner, $imBorderCorner*2-2, $imBorderCorner*2-2, $whiteArray, 0, 0); // top-right corner
imageSmoothArc($im, $imBorderCorner, $imBorderCorner+$height-$imBorderCorner*2-1, $imBorderCorner*2, $imBorderCorner*2, $blackArray, 180, 180); // bottom-left corner
imageSmoothArc($im, $imBorderCorner, $imBorderCorner+$height-$imBorderCorner*2-1, $imBorderCorner*2-2, $imBorderCorner*2-2, $whiteArray, 180, 180); // bottom-left corner
imageSmoothArc($im, $width-$imBorderCorner-1, $imBorderCorner+$height-$imBorderCorner*2-1, $imBorderCorner*2, $imBorderCorner*2, $blackArray, 270, 270); // bottom-right corner
imageSmoothArc($im, $width-$imBorderCorner-1, $imBorderCorner+$height-$imBorderCorner*2-1, $imBorderCorner*2-2, $imBorderCorner*2-2, $whiteArray, 270, 270); // bottom-right corner
imageline($im, 0, $imBorderCorner, 0, $height - $imBorderCorner, $black); // left img border
imageline($im, $width-1, $imBorderCorner-5, $width-1, $height - $imBorderCorner+3, $black); // right img border
imageline($im, $imBorderCorner-4, 0, $width - $imBorderCorner+5, 0, $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), $blackArray, 0, 2*M_PI);
imageSmoothArc($im, $xoff+($bwidth/2), $yoff+$bheight+($bulb/2), $bulb, $bulb, $colGotArray, 0, 2*M_PI);
# draw top circle
$topcolor = $got==200 ? $colGotArray : $colNotGotArray;
imageSmoothArc($im, $xoff+($bwidth/2)-1, $yoff, $bwidth+($border*2)-1, $bwidth+($border*2), $blackArray, 0, 2*M_PI);
imageSmoothArc($im, $xoff+($bwidth/2)-1, $yoff, $bwidth-2, $bwidth, $topcolor, 0, 2*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($im, 8, 0, 62, ($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($fontSize, 0, "fonts/$fontFamily", $text);
$targetTextXoff = $targetTextBox[0] + $targetTextBox[2] / 2;
return imagettftext($im, $fontSize, 0, $xoff+($bwidth/2)-$targetTextXoff, $yoff+$bheight+($bulb/2)+$yoff2, $color, "fonts/$fontFamily", $text);
}
writeTextCenter($im, 12, 'FreeSansBold.ttf', $yearDisp, ($yoff+$bheight+($bulb/2))*-1 + 22,$black, $xoff, $bwidth, $yoff, $bheight, $bulb); // 18
writeTextCenter($im, 9, 'FreeSansBold.ttf',$pcText, 6, $black, $xoff, $bwidth, $yoff, $bheight, $bulb);
writeTextCenter($im, 9, 'FreeSans.ttf', 'Target', 36, $black, $xoff, $bwidth, $yoff, $bheight, $bulb);
writeTextCenter($im, 10, 'FreeSansBold.ttf', $targetText, 52, $black, $xoff, $bwidth, $yoff, $bheight, $bulb);
writeTextCenter($im, 9, 'FreeSans.ttf', 'Raised', 68, $black, $xoff, $bwidth, $yoff, $bheight, $bulb);
writeTextCenter($im, 10, 'FreeSansBold.ttf', $raisedText, 84, $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 += 1 ) {
$y = $b * sqrt( 1 - ($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 += 1 ) {
$x = $a * sqrt( 1 - ($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, $start, 2*M_PI);
imageSmoothArc ( &$img, $cx, $cy, $w, $h, $color, 0, $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;
}
}
}
}
}
?>