[Tutorial] Make a dynamically generated forum sig using PHP....

Associate
Joined
30 Apr 2003
Posts
1,794
Location
The dark side of the moon
This tutorial will show you how to dynamically add some random text/quotes over an existing background so you can have a truly dynamic PHP generated image as your forum sig.

Output:
tutorial.php


or:

random.php


To do this requires several things:
  • Hosting with PHP enabled
  • Hosting with GDLib installed.
  • A basic text editor - Notepad will be sufficient

1. To check if you have GDLib support on your hosting, add the following code to a file called phptest.php:
Code:
<?php
phpinfo();
?>

Scroll down to the 'gd' section and look for this:
1.png


If GD Support is 'enabled' then you should have no problems generating images on the fly. If it is shows 'disabled' then it will not work.

** If any one wants hosting for their dynamic sig because their host doesn't provide it, ask in this thread, or email me [email protected]**

2. Photoshop a background image. Add hilarious images for added comedy value.

e.g
bg.png


3. Save this image in a directory and call it bg.png

4. Next step is to add some quotes to a text file. This is the text that will be randomly selected and printed over our background image. Open Notepad, write some 'stuff' and save as quotes.txt
 
Last edited:
Associate
OP
Joined
30 Apr 2003
Posts
1,794
Location
The dark side of the moon
5. Open notepad and enter the following PHP code:

Code:
<?php
header ("Content-type: image/png");
$im = imagecreatefrompng ("bg.png");
$colour1 = ImageColorAllocate ($im, 0, 0, 0);
$contents = file("quotes.txt");
$read1 = mt_rand(0,(count($contents)-1));
$input1 = trim($contents[$read1]);
ImageString($im, 2, 18, 15, $input1, $colour1);
imagepng($im);
?>

Save this as random.php in the same directory as bg.png and quotes.txt

I'll explain what each line does so you can edit it accordingly:

Code:
<?php
header ("Content-type: image/png");
Declares the type of image we wish to generate - no need to alter this in any way.

Code:
$im = imagecreatefrompng ("bg.png");
The name of the background image we wish to use.

Code:
$colour1 = ImageColorAllocate ($im, 0, 0, 0);
Declares a text colour. Here it is set to 0,0,0, the RGB values for black. Change these to alter the colour of your outputted text.

Code:
$contents = file("quotes.txt");
Declares the name of the text file which contains all our quotes.

Code:
$read1 = mt_rand(0,(count($contents)-1));
Reads the file and selects a random line.

Code:
$input1 = trim($contents[$read1]);
Trims the randomly selected line so you don't get funny symbols at the end. If you want, you can leave out the code that reads a random line from the next file, an instead

use other PHP variables such as date(), time() etc

See the PHP manual here for all the different functions available.

Code:
ImageString($im, 2, 18, 15, $input1, $colour1);
The ImageString function and it's variables. The 1st variable - '2' is the font size. The 2nd variable is the x-position of the text (from the left of the image), and the 3rd variable is the y-position (from the top of the image).

The 4th variable '$input1' is the text we want to print to the image, and '$colour1' is the colour we want out text to be which we declared on the 4th line.

Code:
imagepng($im);
Returns the dynamically generated image to the browser

6. Upload quotes.txt, bg.png and random.php to the same directory in your webspace.

7. Use the following code to add the image to your forum sig:

b]


Output:
tutorial.php


or:

random.php


The possibilities are endless :).

Notes:
Be aware that because the image is dynamically generated (ie. not cached) everytime someone looks at one of your posts, bandwidth is quite a bit higher than for a normal *.jpg/*.gif sig.

My sig which is <2kb in size used >120mb last month which equates to over 60,000 hits :eek:.

Also make sure your sig complies with the OcUK sig rules which can be found in the FAQ.

Hope some people might find this useful ;).

If you need any help making your own, post in this thread for slightly more advanced things like splitting your quotes over several lines etc.

--Veritas.
 
Last edited:
Associate
OP
Joined
30 Apr 2003
Posts
1,794
Location
The dark side of the moon
^^ There is a far simpler war to split a quote over several lines, like in my sig at the monent, though it is a bit crude:
Code:
$quote1 = split('[#]', $input1);
ImageString($im, 2, 8, 5, $quote1[0],   $colour1);
ImageString($im, 2, 8, 17, $quote1[1],   $colour1);
ImageString($im, 2, 8, 29, $quote1[2],   $colour2);
This simply splits the lines whenever it finds the '#' charachter in quotes.txt and adds the lines to an array which can be called accordingly.
 
Associate
OP
Joined
30 Apr 2003
Posts
1,794
Location
The dark side of the moon
Originally posted by dj_binks
I want that but dont know where to get it :(

I'm working on some code at the moment, I'll post it some time this evening.

Originally posted by robmiller
Just out of interest... how do you make text bold?

I wondered the same thing myself, but couldn't find anything. You can shadow text simply enough by printing the line twice, but with a slight offset and a lighter colour.

I'll keep looking though :).
 
Associate
OP
Joined
30 Apr 2003
Posts
1,794
Location
The dark side of the moon
Originally posted by dj_binks
I want that but dont know where to get it :(

Try this, works fine when I implemented into my sig:

Code:
$filename = 'hits.txt';
$fr = fopen($filename, "r");
$contents = fread($fr,filesize($filename));
fclose($fr);

$fr = fopen($filename,"w");
fwrite($fr, $contents + 1);
fclose($fr);

$hitsString = "This sig has been viewed ".$contents." times.";
 
Back
Top Bottom