Jump to content


Photo

Donations thermometer / Graphics meets maths meets GRRRrrrrr


  • Please log in to reply
11 replies to this topic

#1 Mouse

Mouse
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts
  • LocationToo Close to LONDON

Posted 24 August 2006 - 05:34 PM

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…)

<?
// 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);
?>

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

Many thanks

Mouse


#2 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 24 August 2006 - 05:45 PM

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

http://www.entropyfa...oftware/thermo/

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
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#3 Mouse

Mouse
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts
  • LocationToo Close to LONDON

Posted 25 August 2006 - 05:05 AM

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

http://www.entropyfa...oftware/thermo/

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

most excelent... why didn't i find this... many thanks

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,020 posts

Posted 25 August 2006 - 07:07 AM

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);
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 Mouse

Mouse
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts
  • LocationToo Close to LONDON

Posted 25 August 2006 - 07:10 AM

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);

Many thanks i'll give this a spin tonight when i get 127.0.0.1 many thanks

Mouse

#6 Mouse

Mouse
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts
  • LocationToo Close to LONDON

Posted 25 August 2006 - 09:00 PM

"The image “http://localhost/atm-v5/thermo.php” cannot be displayed, because it contains errors." Hmmm... needs work (note to self: maybe i should just do this in photoshop...!)

Any One?

Mouse

#7 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 25 August 2006 - 09:14 PM

Try emailing the person who wrote it.  They may be able to help.

Is the GD graphics library working ok?  Does it say what errors.  it too me a couple attempts to get it working but it does work....look at http://www.jackgodfrey.org.uk
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#8 dark dude

dark dude
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 25 August 2006 - 09:35 PM

"The image “http://localhost/atm-v5/thermo.php” cannot be displayed, because it contains errors." Hmmm... needs work (note to self: maybe i should just do this in photoshop...!)

Any One?

Mouse


I dont believe images use the .php extension ;)

Php = Script extension, an Image extension = .gif, .jpg, .png, .tif, .bmp, .psd etc

#9 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 25 August 2006 - 09:40 PM

This srcipt uses small images to give the impression that the thermoter moves depending on the variable values
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#10 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 25 August 2006 - 09:56 PM

Barand's script .. with a couple of edits so variable names match, etc. Works perfectly.

<?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);
?>

Legend has it that reading the manual never killed anyone.
My site

#11 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,020 posts

Posted 25 August 2006 - 10:10 PM

Here's the one I usually use, passing max and val as variables

:: bar.php ::
<?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);

?>

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

Dark_Dude: Note the .php extension for a dynamicaly produced image
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#12 Mouse

Mouse
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts
  • LocationToo Close to LONDON

Posted 26 August 2006 - 04:04 PM

AndyB, Barand... Gentlemen and scholars both!

thanks chaps....




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users