News:

Looking for a good read? Check out the SimpleDesk Team Blog.

Main Menu

PHP GD Images

Started by tyty1234, February 04, 2010, 01:26:12 AM

Previous topic - Next topic

tyty1234

Okay, I can't stop looking at these awesome progress bars!!! XD

Can someone give me a tutorial on how to handle the PHP GD library please and how to achieve a similar task to the progress bar? I mean, all I can do, and all I've done, is stretched an image based on the percentage given, which is what I've done with DP's progress bar here...



I'm getting kinda bored of it, and I'm not asking for the source code of SD's progress bar, but can someone guide me in the right direction? Thanks. :)
tyty1234
Co-Founder of Dream Portal

cσσкιє мσηѕтєя

What Arantor did for the latest progress bars was mostly taking premade artwork I gave him and added GD generated bars to it, basically just a colored square sized according to the percantage. Then fun thing is making the gradients,I haven't looked at that yet. :P
The original one we used was just a simple bar with different colored texts and two images (logo and checkmark) merged onto it.

We're planning to release the code of our first bar along with a short guide, so keep your eyes open :)

Check out www.php.net/gd for a list of functions you can use.

tyty1234

Thanks for the info Nas. I figured out once I visited the GD page, so I'm gonna go try see what I can fry in the pot. ;)
tyty1234
Co-Founder of Dream Portal

Gruffen

It really depends on what you're trying to do. GD is very very powerful if used correctly.

Let me give you a run down on how they, broadly, work. I'm going roughly in order of complexity rather than any other order. In all cases, the code prior to it loads the current issues on the tracker for the current version, loads the solved issues and stores the total as a figure in $percent (so I'll be referring to that in the following) - $percent is an integer from 1-100. Where there is a bar, x1,y1 = the top left, x2,y2 = bottom right. There may have been the odd +1 or -1 in the code to give it the precise look, but I'm not getting into that for simplicity's sake.



This is by far the simplest image to generate, because it's almost entirely pre-made artwork from Nas. The image is the entire background with the progress bar at 100% pre-filled.

  • Load the image from png file
  • imagefilledrectangle in black from x1 + ($percent * (x2 - x1)), y1 to x2,y2




  • Create a blank indexed image (I think... can't remember)
  • imagefilledrectangle, black from x1,y1 to x2,y2
  • imagefilledrectangle, orange from x1,y1 to x1 + ($percent * (x2 - x1)),y2
  • imagestring the substrings (Simple in blue, Desk in orange, rest in black)




  • Create a blank true-colour image
  • imagefilledrectangle, black from x1,y1 to x2,y2
  • imagefilledrectangle, orange from x1,y1 to x1 + ($percent * (x2 - x1)),y2
  • imagettftext twice for each substring (1.0 / is / 43% / closer to release!), once for the dropshadow (grey, IIRC), one for the top level, aligned one pixel up and left from the dropshadow
  • imagecopyresample calls to copy/paste the SimpleDesk logo and the checkmark into the image (using that to preserve transparency)




  • Load the background PNG
  • imagefilledrectangle, black from x1,y1 to x2,y2
  • imagefilledrectangle, orange from x1,y1 to x1 + ($percent * (x2 - x1)),y2
  • imagettftext in black for the 1.0 is 43% complete!




  • Load the background PNG
  • imagefilledrectangle, black from x1,y1 to x2,y2
  • Loop from x1 to ($percent * (x2 - x1)) as $i
  • Knowing the start (blue) and end (orange) colours, figure out the distance between the colours (literal distance between RGB components) based on $percent and draw an imagefilledrectangle from $i - 0, y1 to $i,y2
  • imagettftext in black for the 1.0 is 43% complete!



This is a little more complex. A literal gradient of RGB components from red (255/0/0) to green (0/255/0) doing an equal shift per step will hit grey (128/128/0) at the mid point, which looks awful. So I coded up something that could handle being given a mid-point (this is gradient3 because it's three colours, red->yellow->green, which is a colour shift from 255/0/0 to 255/255/0 to 0/255/0)


  • Load the background PNG
  • imagefilledrectangle, black from x1,y1 to x2,y2
  • Loop from x1 to ($percent * (x2 - x1)) as $i
  • Figure out whether we're between colours 0 and 1, or 1 and 2
  • Knowing the start (red or yellow) and end (yellow or green) colours, figure out the distance between the colours (literal distance between RGB components) based on $percent and draw an imagefilledrectangle from $i - 0, y1 to $i,y2
  • imagettftext in black for the 1.0 is 43% complete!

The routine is actually the same routine for both gradients, actually; it's smart enough to handle any number of gradient steps (either blue->orange, or red->yellow->green, or anything else for that matter), since it's just the same thing, really.



This is actually a massively complex piece of code - the odometer itself is actually a separate image canvas, drawn on and copied across. It's actually drawn at 100x100 pixels, so it can be resized and antialiased in the process.


  • Load the background PNG
  • Knowing the scope of the odometer arc (300 degrees, 30 degrees either side of the 180 position), loop from 0 to 299 as $i
  • Figure out the colour (as the gradient above), imagefilledarc from the centre of the image, from $i to $i+1 degrees
  • Draw a black outline around the arc
  • Draw the dividing marks every ten percent, alternating long and short lines
  • imagefilledellipse to the background colour of equal width and height to erase the circle inner
  • Mark the background transparent
  • imagefilledpolygon from either side of the centre to the end point

I won't get into the mechanics of the maths for this; it's pretty heady stuff if you're not used to sin/cos trigonometry, but I can release the code if necessary.

tyty1234

Wow...that's alot of info. Thanks for this Arantor. :)

However, where do I get x1 and x2 from? I know it's at top left and bottom right, but is there like a function or something for it?
tyty1234
Co-Founder of Dream Portal

Gruffen

x1, y1 and x2, y2 are the pixel coordinates on the image. You have to figure out where they are. I looked against the background image in GIMP, for example.


Trekkie101


<?php

$var1 
'variable';

while();

do();

echo
' blah blah';
?>



For chris.

Francis

Quote from: Trekkie101 on February 08, 2010, 10:27:31 PM

<?php

$var1 
'variable';

while();

do();

echo
' blah blah';
?>



For chris.

Wow? Really nothing helpful here Trekkie. GD isn't a matter of variables and biscuits, it's just a big matter of testing all over and over.

Trekkie101

Ooops sorry Francis, that block was just me showing Chris an example of the new Code Block font here, the nicer looking one :P

Francis

Quote from: Trekkie101 on February 09, 2010, 07:57:01 AM
Ooops sorry Francis, that block was just me showing Chris an example of the new Code Block font here, the nicer looking one :P
Oh so that is why there is almost no white spaces and is in bold. :)

Trekkie101

Quote from: Francis on February 09, 2010, 06:11:31 PM
Quote from: Trekkie101 on February 09, 2010, 07:57:01 AM
Ooops sorry Francis, that block was just me showing Chris an example of the new Code Block font here, the nicer looking one :P
Oh so that is why there is almost no white spaces and is in bold. :)

What are you on about? :P

Francis

Quote from: Trekkie101 on February 09, 2010, 07:16:11 PM
Quote from: Francis on February 09, 2010, 06:11:31 PM
Quote from: Trekkie101 on February 09, 2010, 07:57:01 AM
Ooops sorry Francis, that block was just me showing Chris an example of the new Code Block font here, the nicer looking one :P
Oh so that is why there is almost no white spaces and is in bold. :)

What are you on about? :P
In that new design, the letters look like they are overlaying on the next ones and so on. And the bold doesn't help my young eyes! LOL. :)

Trekkie101

Could you post a screenshot? It seems clearer in every config I've checked  :o

[FailSafe]

It looks fine for me. '.'