Free Ringtones - Ogłoszenia,praca - Big Brother 9 - Mortgage - Freelance

PDA

View Full Version : Making Images With PHP


digitalpoint
Mar 22nd 2004, 11:10 am
One of the really neat things about PHP (one of the many) is the ability to dynamically generate images by using the GD lib (although it's compiled in by default, so you don't need to know anything about it).

A simple example would be to display Yahoo WebRank graphic (http://www.digitalpoint.com/tools/webrank/graphic.php?wr=4). The wr= part of the URL lets you get an image that ranges from 0-10 and display the bar graph.

So how is this done?

I made it easy on myself and made 3 different images that PHP pieces together. The background image, the bar part of the graph, and the top of the bar graph.


The graphic.php file:
<?php
$im = ImageCreateFromPNG ("bg.png");
$bar = ImageCreateFromPNG ("bar.png");
$bartop = ImageCreateFromPNG ("bartop.png");

for ($i = 1; $i <= $wr; $i++) {
ImageCopy ($im, $bar, 4, 62 - (6 * $i), 0, 0, 40, 6);
}

if ($wr > 0) ImageCopy ($im, $bartop, 4, 62 - (6 * $wr), 0, 0, 40, 4);

$col_mgrey = ImageColorAllocate ($im, 100, 100, 100);
$col_lgrey = ImageColorAllocate ($im, 200, 200, 200);

ImageString($im, 5, 20, 4, $wr, $col_mgrey);
ImageString($im, 5, 21, 5, $wr, $col_lgrey);

Header ("Content-type: image/png");
ImagePng ($im);

ImageDestroy ($bar);
ImageDestroy ($bartop);
ImageDestroy ($im);
?>

I won't get into complex image creation, but it can be done as well... an example is the attachment, which shows new users per day (from the keyword tracker) as of right now, with a unique line per language. Another interesting thing about the way that one is done is a SQL handle is fed to the function, so anything you can query with a SQL statement can be graphed.

- Shawn

Mr T
Mar 22nd 2004, 11:21 am
Good work, more on PHP image abilities - last night I was amazed at how easy it was to create a thumbnail size copy of a user uploaded image, then you can show the small one (saving bandwidth) on a page as a link to the bigger one. Slightly off topic, just was incredibly pleased with my coding last night :)

rustybrick
Mar 22nd 2004, 12:32 pm
Cool stuff.

cosminx2003
Apr 6th 2008, 5:08 am
Really cool.