Dynamic Signature - Get Data from Another Site and Show it in PHP

In this tutorial, I will show you how you can obtain data from an external site and put it on your desired background, to use as a signature. Since this tutorial will just be an example, it will be gathering information from TutorialToday.com (specifically the 5 latest tutorials) and listing out the titles in order. Although, you can also use this method to show stats from a game you play (assuming it posts stats online) or any other dynamic stat or feed on another site.

The actual code for this tutorial will be pretty short, but complex. First, save this background, which will be used with the example. It isn't anything special because I am not much when it comes to graphics. Save it as background.jpg.



Now here is the code for gathering the data, writing it to the image, and outputting it. Save this as sig.php.

<?php
// retrieve the data
$site file_get_contents("http://www.tutorialtoday.com/index.php");
$regex '/<td colspan="3" class="smalltext" style="color:#7ca5c8;"><strong><a target="_blank" href="(.*)" style="color: #7ca5c8;">(.*)</a></strong></td>/';
preg_match_all($regex$site$results);

// get background
$im imagecreatefromjpeg("background.jpg");

// text color (blue)
$textcolor imagecolorallocate($im00255);

// write the strings
$i 1;
while(
$i 6) {
    
$x $i 2;
    if(
strlen($results[2][$x]) > 30) {
        
imagestring($im310020 + ($i 15), $i.'. '.substr($results[2][$x], 030).'..'$textcolor);
    }else{
        
imagestring($im310020 + ($i 15), $i.'. '.$results[2][$x], $textcolor);
    }
    
$i++;
}

// ouput the image
header("Content-Type: image/jpeg");
imagejpeg($im);
?>




Code Breakdown:

$site = file_get_contents("http://www.tutorialtoday.com/index.php"); - This will connect to your favorite tutorial site and copy the contents to the variable $site.

$regex = '/<td colspan="3" class="smalltext" style="color:#7ca5c8;"><strong><a target="_blank" href="(.*)" style="color: #7ca5c8;">(.*)</a></strong></td>/'; - This probably looks more complex than it really is, it is the regex which we will use with the function preg_match_all(). It will find all the matches in the page and put it into an array, so we can access it and find the titles of the tutorials.

preg_match_all($regex, $site, $results); - This is the function which will find us the titles. Tt takes $regex (the pattern to search), $site (the contents to search), $results (the array to output it in).

$im = imagecreatefromjpeg("background.jpg"); - This creates a handle for the image, and in this case we will be accessing the image with the variable $im.

$textcolor = imagecolorallocate($im, 0, 0, 255); - This is just as it says, it is choosing a color for the text and returning an identifier for the color to the variable $textcolor. Note: This function takes the color in a RGB (Red Green Blue) format, so in this case, the color is blue.

$i = 1; - This variable will be used with our loop, so we know when to stop, it is 1 instead of 0 because it is also going to be used to number each line.

while($i < 6) { - This may be confusing because we are showing 5 but this says 6, it is because $i is starting at 1 instead of 0.

$x = $i + 2; - The $x variable will be used with the $results array, so we know which title to show.

if(strlen($results[2][$x]) > 30) { - This is just checking if the title is longer than 30 characters, because if it is, it would probably run off of the background, so if the title is greater than 30 characters there will be a '..' attached to the end, to show the title is longer than what we show.

You may have also noticed $results[2][$x], this is a multi-dimensional array, which simply means it is arrays inside of arrays.

imagestring($im, 3, 100, 20 + ($i * 15), $i.'. '.substr($results[2][$x], 0, 30).'..', $textcolor); - This will write the line to the image.

The first parameter takes the image identifier.
The second takes the font size between 1-5.
The third takes the x-coordinate from the top-left corner.
The fourth takes the y-coordinate from the top left corner, notice this adds $i multiplied 15 to it, which basicallys means that each line will start 15 pixels down from the previous.
The fifth takes the actual string, and here is just puts the line number ($i) then a dot and the title.
The last parameter takes the color which we set as blue.

header("Content-Type: image/jpeg"); - This changes the content type so we will output an image, instead of text.

imagejpeg($im); - Finally, this will output the result.

Now you can display your dynamic signature on a forum or a site, you can just put the source of the image to the .php script and it will show as an image.

If you are wanting to make your own custom signature then I recommend looking at this regex cheat sheet, if you don't really know how to use regex. Also, it can get somewhat confusing with the multi-dimensional array for the results, so try a print_r($results) if you are having trouble finding where exactly the value you want is in the array.

Here is my result (this result is a static image, it shows the latest tutorials at the time of making this tutorial) :



I hope you enjoyed this tutorial and are successful in making any modifications to the script for your own purpose.

Pages (1) 1
Tutorial Stats

Date Added: 23-02-2008
Votes: 5
Rating: 8
Views: 8141

Author: admin

Main Category: PHP
Sub-Category: Images



Copyright TutorialToday.com Powered by TutorialToday.com