Latest from Twitter: Changed my avatar. It is actually me, well more lens than me. However I still think it is the first time I've used an avatar that is me.
 

PHP Image from RSS

I recently had the urge to create an forum signature from my RSS feed. My first step was to find a open source RSS parser that is done in PHP. I found a little application called MagpieRSS. This little script parses any RSS feed and puts them into an array that you can use.

I took this array and created a script to to take that data and put into an image. The code below is that script:

// Grab the magpie script
require_once('magpierss/rss_fetch.inc');

// Set this to the RSS feed you want to parse
$url="http://feeds.feedburner.com/tech-geek";

$background="sig_bg.png";

$rss = fetch_rss($url);

// create a image
$im = imagecreatefrompng($background);

//set the text colors
$textcolor = imagecolorallocate($im, 200, 200, 200);
$textcolor2 = imagecolorallocate($im, 255, 255, 255);

// Add text to the image
imagestring($im, 3, 160, 1, "Latest Posts", $textcolor2);
imagestring($im, 2, 160, 15, $rss->items[0][title], $textcolor);
imagestring($im, 2, 160, 28, $rss->items[1][title], $textcolor);
imagestring($im, 2, 160, 40, $rss->items[2][title], $textcolor);

// output the image
header("Content-type: image/png");
imagepng($im);

// Destroy the image from the servers cache
// This set is optional, but it saves quite a bit of memory by killing it
imagedestroy($im);

?>

The issue with using a dynamic php image is that many forum’s will not allow it. The fix for this is to use mod_rewrite to make it look like the file is a PNG rather than PHP. To do this you just need to create (or edit) your .htaccess file if running Apache and put the following in:

RewriteEngine on

# You can use anything you would like on the line below.
# Where it says sig.png is the file name you will call remotely
# sig.php is the name of your script
RewriteRule sig.png sig.php

To run this script all you will need to do is run http://yourserver/sig.png which will rewrite that URL to sig.php. This means that any site checking to see if the file extension is an actual image. phpBB is a popular forum that seems to love to check that the file type is a real image.

No Comments

Leave a comment


Most Popular Posts