[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:
Soldato
Joined
22 May 2003
Posts
4,055
very good tutorial!

Wonder if you or someone can help me tho, i am getting this error when i try to view the page (it is on my own server)

Warning: Cannot modify header information - headers already sent by (output started at c:\inetpub\wwwroot\testbed\sigs\sigtext.php:2)
in c:\inetpub\wwwroot\testbed\sigs\sigtext.php on line 3

‰PNG  …h•ÿ.áP†%"Í ’¬·4Ò–ˆ¬ð¶¼L^¬ø©úÂI÷Fb––ˆÔåG«:‚˜5Ò÷#›–ˆ ‡¼¸ñƒ*`‰È0ÂÊ4x¦Á°DdÜü˜5âªP
KDZCç°D¤.òßfüê×ÆQÀ‘±#_7³Ñcœ–„"Ò X"Ò X"Ò X""""MûW}!º›ê)‡

Any ideas?

TIA
 
Soldato
Joined
26 Dec 2003
Posts
16,522
Location
London
Originally posted by BruceLee
very good tutorial!

Wonder if you or someone can help me tho, i am getting this error when i try to view the page (it is on my own server)

Warning: Cannot modify header information - headers already sent by (output started at c:\inetpub\wwwroot\testbed\sigs\sigtext.php:2)
in c:\inetpub\wwwroot\testbed\sigs\sigtext.php on line 3

‰PNG  …h•ÿ.áP†%"Í ’¬·4Ò–ˆ¬ð¶¼L^¬ø©úÂI÷Fb––ˆÔåG«:‚˜5Ò÷#›–ˆ ‡¼¸ñƒ*`‰È0ÂÊ4x¦Á°DdÜü˜5âªP
KDZCç°D¤.òßfüê×ÆQÀ‘±#_7³Ñcœ–„"Ò X"Ò X"Ò X""""MûW}!º›ê)‡

Any ideas?

TIA



This indicates that something (whitespace usually) comes before the opening PHP tag, or you're echoing something before the header() function.
 
Soldato
Joined
22 May 2003
Posts
4,055
Originally posted by robmiller
This indicates that something (whitespace usually) comes before the opening PHP tag, or you're echoing something before the header() function.

Ahah!!

you sir, are a star, I have been having those problems before on other .php stuff.

Cheers it works now! :)
 
Soldato
Joined
22 May 2003
Posts
4,055
thanks for the offer, at present I dont have php hosting, but possibly planning in the summer if i get to grips with it. If i get carried away with this i might ask ya :)

Just trying to get the basics on my home server, installing php was an ordeal in itself :p

EDITED for the Queens speach to comply with current enforcement on these here forums :o
 
Soldato
Joined
26 Dec 2003
Posts
16,522
Location
London
By the way, I've been working on a little one that works with line breaks so you can have long quotes, and it works in any font. Just change C:\Windows\Fonts to the path to your TTF file, it can be in any directory :) It's got a couple of flaws, but it works alright.

Code:
<?php

header ("Content-type: image/png");

$im = imagecreatefrompng ("bg.png");

$colour1 = ImageColorAllocate ($im, 0, 0, 0);

	$quote = 
	
	$var = $quote['0']['quote'];

        $contents = file("quotes.txt");
        $read1 = mt_rand(0,(count($contents)-1));
        $var = trim($contents[$read1]);
	
	
	$exp = explode(" ", $var);
	
	$count = count($exp);
	
	if($count > 10) {
		
		for($i=0; $i<10; $i++) {
			
			$var1 .= $exp [$i] . " ";
			
		}
		
		for($i=10; $i<20; $i++) {
			
			$var2 .= $exp[$i] . " ";
			
		}
		
		for($i=20; $i<30; $i++) {
			
			$var3 .= $exp[$i] . " ";
			
		}
		
	}
	
	else {
		
		for($i=0; $i<$count; $i++) {
			
			$var1 .= $exp[$i] . ' ';
			
		}
		
	}

}

$black = imagecolorallocate($im, 0,0,0);

imagettftext($im, 10, 0, 8, 20, $black, 'C:\Windows\Fonts\GEORGIA.ttf', strip_tags($var1));
if(!empty($var2)) { imagettftext($im, 10, 0, 8, 40, $black, 'C:\Windows\Fonts\GEORGIA.ttf', strip_tags($var2)); }
if(!empty($var2)) { imagettftext($im, 10, 0, 8, 60, $black, 'C:\Windows\Fonts\GEORGIA.ttf', strip_tags($var3)); }

imagepng($im);

imagedestroy($im);

?>
 
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.
 
Soldato
Joined
26 Dec 2003
Posts
16,522
Location
London
Originally posted by Veritas
^^ 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.


Yeah, but I rip my quotes out of a database and they're already in (200+ of them) and so I cba to change them :p


Here it is, in all its glory:

sig.php
 
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