Jump to content

Donations thermometer / Graphics meets maths meets GRRRrrrrr


Mouse

Recommended Posts

Donations thermometer Ladies and gentlemen… good day to you all.

Well its my least favourite subject here… math(s). to be honest it’s the bit in PHP I suck at… and the other bit I know nothing of is graphics in PHP.

What I am aiming to do is generate a horizontal donations thermometer on my site. I think the community site I am building will benefit so here’s what I am aiming to do…

To produce an image X pixels high, by Y pixels wide that will represent the part of the target amount (lets call it Z), which has been met by donations (erm… lets call that D).

So far I got to here (#### ) and lost the will to live (if life involves maths that is…)

[code]
<?
// Add values to the graph
$graphValues=10; // D for Donations random ammount for testing

// Define .PNG image
header("Content-type: image/png");
$imgWidth=300; // Y pixels wide
$imgHeight=10; // X pixels high

// Create image and define colors
$image=imagecreate($imgWidth, $imgHeight);
$colorWhite=imagecolorallocate($image, 255, 255, 255); // Background Colour
$colorBlue=imagecolorallocate($image, 104, 157, 228); // Border Colour
$colorRed=imagecolorallocate($image, 255, 51, 0); // Thermomiter Fill Colour

########################################

// Output graph and clear image from memory
imagepng($image);
imagedestroy($image);
?>
[/code]

If any kind Math / PHP fiend would care to help I would be grateful

Many thanks

Mouse
I don't know if this is any good but I got this fund raising thermoter for my site here

[url=http://www.entropyfarm.org/software/thermo/]http://www.entropyfarm.org/software/thermo/[/url]

I changed some of the sizes of the images and changed some of the variables to make it fit within my site and it does the job i want
[quote author=AdRock link=topic=105508.msg421498#msg421498 date=1156441531]
I don't know if this is any good but I got this fund raising thermoter for my site here

[url=http://www.entropyfarm.org/software/thermo/]http://www.entropyfarm.org/software/thermo/[/url]

I changed some of the sizes of the images and changed some of the variables to make it fit within my site and it does the job i want
[/quote]
most excelent... why didn't i find this... many thanks
Your image width is 300px, but you need to know what value the full width represents (ie what is your max value);

Assuming it's 100,

$max = 100;
$graph_value = 10;
$valWidth = ($graph_value/$max) * 300;

imagefilledrectangle($im, 0, 0, $valWidth , $imageHeight, $colorRed);
imagerectangle ($im, 0, 0, $imgWidth - 1, $imageHeight - 1, $colorBlue);
[quote author=Barand link=topic=105508.msg421796#msg421796 date=1156489658]
Your image width is 300px, but you need to know what value the full width represents (ie what is your max value);

Assuming it's 100,

$max = 100;
$graph_value = 10;
$valWidth = ($graph_value/$max) * 300;

imagefilledrectangle($im, 0, 0, $valWidth , $imageHeight, $colorRed);
imagerectangle ($im, 0, 0, $imgWidth - 1, $imageHeight - 1, $colorBlue);
[/quote]
Many thanks i'll give this a spin tonight when i get 127.0.0.1 many thanks

Mouse
[quote author=Mouse link=topic=105508.msg422088#msg422088 date=1156539630]
"The image “http://localhost/atm-v5/thermo[b][color=red].php[/color][/b]” cannot be displayed, because it contains errors." Hmmm... needs work (note to self: maybe i should just do this in photoshop...!)

Any One?

Mouse
[/quote]

I dont believe images use the .php extension ;)

Php = Script extension, an Image extension = .gif, .jpg, .png, .tif, .bmp, .psd etc
Barand's script .. with a couple of edits so variable names match, etc. Works perfectly.

[code]<?php
// Define .PNG image
header("Content-type: image/png");
$imgWidth=300; // Y pixels wide
$imgHeight=10; // X pixels high

// Create image and define colors
$image=imagecreate($imgWidth, $imgHeight);
$colorWhite=imagecolorallocate($image, 255, 255, 255); // Background Colour
$colorBlue=imagecolorallocate($image, 104, 157, 228); // Border Colour
$colorRed=imagecolorallocate($image, 255, 0, 0); // Fill Colour

$max = 100;
$graph_value = 10;
$valWidth = ($graph_value/$max) * 300;

imagefilledrectangle($image, 0, 0, $valWidth , $imgHeight, $colorRed);
imagerectangle ($image, 0, 0, $imgWidth - 1, $imgHeight - 1, $colorBlue);

// Output graph and clear image from memory
imagepng($image);
imagedestroy($image);
?>[/code]
Here's the one I usually use, passing max and val as variables

:: bar.php ::
[code]<?php
// set dimensions
    $w = 300;
    $h = 12;
// create image
    $im = imagecreate($w, $h);
// set colours to be used
    $bg = imagecolorallocate($im, 0xE0, 0xE0, 0xE0);
    $black = imagecolorallocate($im, 0x00, 0x00, 0x00);
    $barcolor  = imagecolorallocate($im, 0xFF, 0xFF, 0x00);
// draw border
    imagerectangle($im, 0,0,$w-1,$h-1,$black);
// get value and max value from query string
    $val = $_GET['val'];
    $max = $_GET['max'];
// calculate dimensions of inner bar
    $barw = $max ? floor(($w-2) * $val / $max) : 0;
    $barh = $h - 2;
// draw inner bar
if ($barw)
    imagefilledrectangle($im, 1, 1, $barw, $barh, $barcolor);
// send image header
    header("content-type: image/png");
// send png image
    imagepng($im);
    imagedestroy($im);

?>[/code]

Place on page with
[code]
<img src='bar.php?val=30&max=100'>
[/code]

Dark_Dude: Note the .php extension for a dynamicaly produced image

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.