PDA

View Full Version : Dynamic Images


mck9235
03-05-2005, 11:37 PM
This tutorial shows you how to create a dynamic image using PHP. First lets show the actual file:

<?php
$dayToCountDownTo = 1;
$daysAway = $dayToCountDownTo - intval(date("z"));
$text = "Your text here " . strval($daysAway);
$fontSize = 7;
$ImageWidth = imagefontwidth($fontSize) * strlen($text);
$ImageHeight = imagefontheight($fontSize);
$Image = imagecreatefromjpeg("http://www.bg.com/background.jpeg");
$BgColor = imagecolorallocate($Image, 255, 255, 255);
imagecolortransparent($Image, $BgColor);
$TextColor = imagecolorallocate($Image, 0, 114, 173);
imagestring($Image, $fontSize, 0, 0, $text, $TextColor);
header("Content-type: image/png");
imagepng($Image);
imagedestroy($Image);
?>


There is only a couple things you need to edit, the first is the total days until your countdown, then your text, then the font size, then on this line:

$Image = imagecreatefromjpeg("http://www.bg.com/background.jpeg");

Change the URL to the URL of your desired background image.
Next we need the color of the font, this is done in RGB
$TextColor = imagecolorallocate($Image, 0, 114, 173);
Edit the three numbers, leaving the formatting the same!
Check this page out for many common RGB values: http://www.w3schools.com/html/html_colors.asp

Okay now you should be done editing the file, save it as image.php and upload it to your server.
Now lets look at the code to display the image:

<table width="360" height="250">
<tr>
<td valign="middle" style="background-image: url(‘http://www.placehwerebgimageis.com);"><img src="http://www.placewherephpfileis.com " />
</td>
</tr>
</table>
As you see you need to edit three things, in the top line the two values, change those to the width and height of your background image. The first URL is the URL of your background image and the second is the URL of the PHP file we made above. Once you edit those two just save it and upload it, then you can acess the image by:
http://www.whereuploaded.ext/phpfile.php?ext=.png

Having ext=.png enables you to use it in the tags on a forum.
Note: /phpfile,php?ext=.png is the first PHP file.

[i]Code from this tutorial was used with permission from Tim at http://www.tm-software.uni.cc

Sage Tech
03-05-2005, 11:51 PM
Great Stuff Mck (and Tim) the new tutorial section(s) will be good to say the least, and with content like this it will better than even I imagained :D :D

Zalt4n
03-06-2005, 05:57 AM
Having ext=.png enables you to use it in the [IMG] tags on a forum.
Note: /phpfile,php?ext=.png is the first PHP file.

Ooh, thank you so much. I was looking at the PHP help files earlier today to do something like this, but I could not figure it out. Now this gives me a much clearer idea. Thanks. ;)

(I guess this is similar to how Tim creates a 'vSig'.)

DrWolfgangVonBubbles
03-06-2005, 06:51 AM
I am unsure of what this does. Does it make an image and put text over it? I am kind of confused.

Also, I'd like to know more about how this works. :P

Could I make a sig with rotating images or an image map in a similar way?

Edit: What is the second PHP file for? What does it do?

Zalt4n
03-06-2005, 09:01 AM
I am unsure of what this does. Does it make an image and put text over it? I am kind of confused.
This is what this example basically does (I think), but it can do a lot more...


Also, I'd like to know more about how this works. :P

Could I make a sig with rotating images or an image map in a similar way?I think you probably could make it with rotating images. I'm unsure of what an image map is...[/quote]

Edit: What is the second PHP file for? What does it do?
Second PHP file? There's only one, isn't there? There is another image file, though, maybe you're confused with that.

url(‘http://www.placehwerebgimageis.com)EDIT: The and symbol, hash adnd number is meant to be a '.
Is the quotation mark before the http a typo? :confused:

If you edited your .htaccess file to read, say, jpeg file extensions as PHP (see here (http://www.tm-software.uni.cc/?act=php-extensions)), could name the PHP file as example.jpeg, and then link to that from a forum image?

Also, is it possible to set transparency, replacing a RGB colour?

mck9235
03-06-2005, 02:48 PM
I am unsure of what this does. Does it make an image and put text over it? I am kind of confused.

Also, I'd like to know more about how this works. :P

Could I make a sig with rotating images or an image map in a similar way?

Edit: What is the second PHP file for? What does it do?
The text on the image will change each day, hence creating a countdown. Its cool. :cool:

As for the rotating images, I know there is a free service which does that, let me dig up the link. :)

The second file makes a table and has your background image as the background image and then the text image thingy is on top. :)

Tim
03-06-2005, 03:10 PM
(I guess this is similar to how Tim creates a 'vSig'.)
No, actually, not at all. I used to create it this way -- and it worked perfectly. Then IPB got smarter, and I had to change to an .htaccess trick, which I'm using now.

I wrote a [small] tutorial on my website (http://www.tm-software.uni.cc/) on how to go about making your own extensions where php is executed. In vSig's case, my file would be named image.vsig (http://vsig.tm-software.uni.cc/image.vsig)

If you edited your .htaccess file to read, say, jpeg file extensions as PHP (see here (http://www.tm-software.uni.cc/?act=php-extensions)), could name the PHP file as example.jpeg, and then link to that from a forum image?

You could make it seem like a .jpeg extension, but whenever I tried that with previous projects it always messed up the image... I think it's better to use a generic extension (.vsig, in my case). There are other ways of getting around this, too. For example, there's a code out there for .htaccess that forces PHP execution in any ASCII file, and with this, you could make the filename without an extension, simulating a directory. When php files have a forward slash and any kind of text after that, it still works as normal. Therefor, with that .htaccess trick, I could make a url for a dynamicly genrated image look like http://www.domain.tld/images/sig.png, or something similar.

Also, is it possible to set transparency, replacing a RGB colour?
Yes, take a look at http://www.php.net/imagecolortransparent :)
So, you could do something like:
<?php
$Image = imagecreate(10, 10);
$TransColor = imagecolorallocate($Image, 255, 255, 255);
imagecolortransparent($Image, $TransColor);
imagepng($Image);
imagedestroy($Image);
?>
And white (RGB: 255, 255, 255) would be transparent for the image with the handle of $Image.

Tim :D