MyBB Community Forums

Full Version: Creating a latest thread signature for MyBB
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
This is a tutorial re-post from my site, MyPureBB. The tutorial stems from the "latest post" signatures that some of the guys contributing to the site are using here at mybb.com Big Grin Just thought I'd share how it works so people can sue them for their own forums.

First things first, the image is a dynamic image generated by PHP. You therefore need to make sure your server has the GD Image Library installed. You can do this by either asking your host or checking the PHP Info of your server via the MyBB ACP (ACP->Tools & Maintenance->View PHP Info). It would also be nice if your host supported the FreeType 2 library for fancy fonts.

Step 1

Our first step is to create a background image for our signature. You can size this however you want and make it look however you wish. It's always best to use quite a plain image though so you can actually read the text that will be added later. The image we user here is below - feel free to use that as a temporary placeholder if you wish

[Image: lastpostbg.png]

You need to record the dimensions of the image you use too as we'll be needing them later.

Step 2

The next step is to create the actual PHP file. Open up your favourite editor (notepad++ is a good one for Windows users. For linux users, try gedit). Create an empty PHP file as so and save it as sig.php

<?php
/*
*    Latest thread signature original code by euantor of MyPureBB
*    http://www.mypurebb.com
*/
?>

The first thing that we need to do in this file is specify our content type (which is obviously an image). We do this like so:

<?php
/*
*    Latest thread signature original code by euantor of MyPureBB
*    http://www.mypurebb.com
*/

//Set the header type to an image
Header ("Content-type: image/png");
?>

Our next step is to grab the actual last thread. We can do this by one of two ways: by connecting to the database or by grabbing it from the RSS feed. I personally use the RSS feed so my generator can be easily adapted to any software. Here's how we do it then (note: replace {URL} with the full URL to your MyBB forum):

<?php
//Set the header type to an image
Header ("Content-type: image/png");

//This gets our RSS feed - change the URL to your own
$feed = file_get_contents("http://{URL}/syndication.php?limit=1");

//Parsing the RSS
$xml = new SimpleXmlElement($feed, LIBXML_NOCDATA);
?>

Right, so we've now got our feed stored in a variable. The next step is to start creating our image. The below code first creates a blank canvas and then writes the background image to it (note that my background is called <em>lastpostbg.png</em> and is sized at <em>380 x 100 pixels</em> - you may need to change these values if your background is different):

<?php
/*
*    Latest thread signature original code by euantor of MyPureBB
*    http://www.mypurebb.com
*/

//Set the header type to an image
Header ("Content-type: image/png");

//This gets our RSS feed - change the URL to your own
$feed = file_get_contents("http://{URL}/syndication.php?limit=1");

//Parsing the RSS
$xml = new SimpleXmlElement($feed, LIBXML_NOCDATA);

//Create a plain background image to draw onto. If GD can't be initialised, there's a problem
$img = @imagecreate(380, 100) or die('Cannot Initialize new GD image stream');

//Put our real BG onto the canvas		
$img_bg = imageCreateFromPNG("lastpostbg.png");
imagecopy($img, $img_bg, 0, 0, 0, 0, 380, 100);
?>

Now we've got a plain image with our background painted onto it. Our next step is to specify some information about what font we're using and write the text out (you can use any .TTF font so long as you have FreeType 2 installed on your server - otherwise, comment out the line that reads imagettftext and uncomment the line after it).

<?php
/*
*    Latest thread signature original code by euantor of MyPureBB
*    http://www.mypurebb.com
*/

//Set the header type to an image
Header ("Content-type: image/png");

//This gets our RSS feed - change the URL to your own
$feed = file_get_contents("http://{URL}/syndication.php?limit=1");

//Parsing the RSS
$xml = new SimpleXmlElement($feed, LIBXML_NOCDATA);

//Create a plain background image to draw onto. If GD can't be initialised, there's a problem
$img = @imagecreate(380, 100) or die('Cannot Initialize new GD image stream');

//Put our real BG onto the canvas		
$img_bg = imageCreateFromPNG("lastpostbg.png");
imagecopy($img, $img_bg, 0, 0, 0, 0, 380, 100);
		
//Set up our fonts/colours - colours are defined in terms of RGB.
$black = imagecolorallocate($img, 0, 0, 0);
putenv('GDFONTPATH=' . realpath('.'));
$fontbold = 'DroidSans-Bold';

//Write text onto the canvas. This may not work as not every host has Freetype support.
//If it fails comment out the first line by adding a // in front of it and uncomment the second one.
imagettftext($img, 11, 0, 30, 43, $black, $fontbold, $xml->channel->item[0]->title);
//imagestring($img, 5, 30, 43, $xml->channel->item[0]->title, $black);
?>


Our final step is to now create the actual image. This is very simply done by adding two lines - hence giving the final code as

<?php
/*
*    Latest thread signature original code by euantor of MyPureBB
*    http://www.mypurebb.com
*/

//Set the header type to an image
Header ("Content-type: image/png");

//This gets our RSS feed - change the URL to your own
$feed = file_get_contents("http://{URL}/syndication.php?limit=1");

//Parsing the RSS
$xml = new SimpleXmlElement($feed, LIBXML_NOCDATA);

//Create a plain background image to draw onto. If GD can't be initialised, there's a problem
$img = @imagecreate(380, 100) or die('Cannot Initialize new GD image stream');

//Put our real BG onto the canvas		
$img_bg = imageCreateFromPNG("lastpostbg.png");
imagecopy($img, $img_bg, 0, 0, 0, 0, 380, 100);
		
//Set up our fonts/colours - colours are defined in terms of RGB.
$black = imagecolorallocate($img, 0, 0, 0);
putenv('GDFONTPATH=' . realpath('.'));
$fontbold = 'DroidSans-Bold';

//Write text onto the canvas. This may not work as not every host has Freetype support.
//If it fails to work, comment out the first line and uncomment the second.
imagettftext($img, 11, 0, 30, 43, $black, $fontbold, $xml->channel->item[0]->title);
//imagestring($img, 5, 30, 43, $xml->channel->item[0]->title, $black);

//Create the actual image
imagepng($img);
imagedestroy($img);
?>

Closing up

There you have it, a simple latest thread signature for MyBB. Once you've finished, upload the PHP script, the background image and your chosen .ttf font to a folder on your website and test away - all should work perfectly. If you're having problems, however, feel free to contact me for help.

Also feel free to grab the complete package (minus the font file - we're not allowed to re-distribute that) from the MyPureBB post or from the below atatchment.
Nice tutorial, man!
It looks better than other "last post"-sigs.
Thanks a lot Aries-Belgium Smile The nice fonts will work on most hosts, though I have come across a few without FreeType available in the past.
Awesome Euan.....this is really nice tut.
Thanks envira Big Grin
Really cool stuff. Good work! Smile

I'd consider splitting that big comment into a second line though, it's ruining the forum layout.
Ah yes, I'll sort that haha.

Thanks for the compliment Big Grin
Nice tutorial.
Cheers yaldaram Big Grin
Nice Smile I was wondering how you did that
Pages: 1 2