[RSS+PHP(?)]RSS Feed in Signature Image...

Geffy said:
This is the code that I did for the OSNN Latest News banner/sigs

it fetches the information from the RSS feed and parses it, the charData function is the place where it only gets x number of entries

If I get some time, I might polish this code up a bit

PHP:
<?php
/**********************************
* OSNN VirtuaNews RDF Parser
*
* Author: StealthNinja
* eMail: coder@stealth-ninja.co.uk
*
* Written for: ElectronicPunk
*
* Description:
*  Reads the OSNN RDF backend
*  and then parses that file to
*  obtain the current headlines
*  for inclusion into an image
**********************************/

// variable definitions
// Image Vars
$image = "banner_468x60_var1_back.jpg";        // image dimensions 468x60px
$font  = "verdana.ttf";
$size  = 7;
$horiz = 185;
$vert  = 16;
$maxlen= 60;

// RDF Parsing Vars
$url   = "http://www.osnn.net/modules.php?modname=backend&action=rdf";
$temp  = "temp.xml";
$store = 0;
$count = 0;
$maxent= 4;
$array = array();

// XML parsing functions
function openTag($parser, $name, $attrs) {
    global $store;
     
    if ($name == "TITLE") {
        $store = 1;
    } else {
        $store = 0;
    }
}

function closeTag($parser, $name) {
    // w00p de doo
}

function charData($parser, $data) {
    global $store, $array;

    if ($store == 1 && $count <= $maxent) {
        $array[] = $data;
        $store = 0;
        $count++;
    }
}

function storeXML($filename, $string) {
    $string = ereg_replace('&amp;', 'and', $string);
    $string = ereg_replace('&', 'and', $string);
    $fp = fopen($filename, "w") or die ("unable to open storage file for writing<br />\nif it exists then chmod {$filename} to 666");
    $write = fwrite($fp, $string);
    fclose($fp);
    return $write;
}

// define the parser
$parser = xml_parser_create();
xml_set_element_handler($parser, "openTag", "closeTag");
xml_set_character_data_handler($parser, "charData");

if (!(storeXML($temp, file_get_contents($url)))) {
    die ("Unable to store OSNN RDF Data");
}
if (!($handle = fopen($temp, "r"))) {
    die ("Unable to open Temp store<br />\nif it exists then chmod {$temp} to 666");
}


while ($data = fread($handle, filesize($temp))) {
    if (!xml_parse($parser, $data, feof($handle))) {
        die (xml_error_string(xml_get_error_code($parser)));
    }
}

xml_parser_free($parser);

// make sure no strings are too long
foreach ($array as $key => $value) {
    if (strlen($value) > $maxlen) {
        $array[$key] = substr($value, 0, 45) . "..";
    }
}

reset($array);

// Image Creation
$imgData = getimagesize($image);
if ($imgData[2] == 1) {
    header("Content-type: image/gif");
    $im = ImageCreateFromGIF($image);
} else {
    header("Content-type: image/jpeg");
    $im = ImageCreateFromJPEG($image);
}

$textc = ImageColorAllocate($im,64,89,132);
$black = ImageColorAllocate($im,0,0,0);

ImageTTFText($im,$size,0,$horiz,$vert,$textc,$font,$array[1]);
ImageTTFText($im,$size,0,$horiz,$vert+12,$textc,$font,$array[2]);
ImageTTFText($im,$size,0,$horiz,$vert+24,$textc,$font,$array[3]);
ImageTTFText($im,$size,0,$horiz,$vert+36,$textc,$font,$array[4]);

if ($imgData[2] == 1) {
    ImageGIF($im);
} else {
    ImageJPEG($im,'',100);
}

ImageDestroy($im);
?>
Geffy.. thanks for this code, you have saved me loads of time and here is what i ended up doing for the members of my CSS clan..


http://www.clan-km.com/sig/

and with a few added bits you i got it to import images and change the text

http://www.clan-km.com/sig/?name=[KM]%20ShadowDog&img=http://www.clan-km.com/components/com_simpleboard/avatars/62.gif

ive also sorted it out to take in any rss feed as well..

http://www.clan-km.com/sig/?url=htt.../rss/newsonline_uk_edition/front_page/rss.xml
 
Nice work with the code adickt, hope you stick around for a bit :)
 
BUMP! :p

How would I get apostrophe's to display correctly in the post titles? As you can see, every post title with an apostrophe in it gets all the characters after the apostrophe cut off.
 
Last edited:
I'll take a look after work, see if I can't improve its character handling.
 
I remembered I wrote a PHP4 RSS Parser class on here a while back so think I'll use that, I think that it properly handles ' characters and such. Checking now.
 
Can someone test this for me. I've not got an overlay image handy.

This version doesn't need a temp.xml file, it also does not seem to have the same issue as the previous code or my RSS_Parser class which seems to ignore text after a ' character for some reason.

Changes:
Supports PNG background images
More easily configurable text colour
Properly supports more than 4 entries into the image file
Properly trims lines longer than $text_max_chars. It will chop off characters $text_max_chars - 15 before adding the "..." string to the end. This should ensure it wont overflow the image file.

Shorter!! Now only 71 lines of code by taking advantage of the xml_parse_into_struct function.

PHP:
<?php
// Image Configuration 
$image_filename     = "banner_468x60_var1_back.jpg";    // Path to background image
$image_fontfile     = "verdana.ttf";                    // Path to font file
$text_font_size     = 7;                                // Size in Points
$text_left_shift    = 185;                              // Number of pixels from the left to place the text
$text_top_shift     = 16;                               // Number of pixels from the top to place the text
$text_max_chars     = 60;                               // Maximum number of charaters to show before adding '...'

$text_colour_red    = 64;                               // Text colour red value
$text_colour_green  = 89;                               // Text colour green value
$text_colour_blue   = 132;                              // Text colour blue value

// RSS Configuration
$rss_feed_url       = "http://www.osnn.net/modules.php?modname=backend&action=rdf"; 
$number_of_entries  = 4;

// Go for code
$parser = xml_parser_create();
xml_parse_into_struct($parser, file_get_contents($rss_feed_url), $values, $tag_index);

$title_tags = array_slice($tag_index['TITLE'], 1, $number_of_entries);

// Image Creation 
$imgData = getimagesize($image_filename);
header("Content-type: {$imgData['mime']}");

switch($imgData[2]) {
    case IMAGETYPE_GIF:
        $im = ImageCreateFromGIF($image_filename);
        $imageOutputFunc = 'ImageGIF';
        break;
    case IMAGETYPE_JPEG:
        $im = ImageCreateFromJPEG($image_filename);
        $imageOutputFunc = 'ImageJPEG';
        break;
    case IMAGETYPE_PNG:
        $im = ImageCreateFromPNG($image_filename);
        $imageOutputFunc = 'ImagePNG';
        break;
    default:
        die ("Unsupported Image Type");
}

$black = ImageColorAllocate($im,0,0,0);
$text_colour = ImageColorAllocate($im, 
    $text_colour_red, $text_colour_green, $text_colour_blue); 

$vertical_offset = 0;

foreach ($title_tags as $i) {
    if (strlen($values[$i]['value']) > $text_max_chars) {
        $title = substr($values[$i]['value'], 0, $text_max_chars - 15) . "...";
    } else {
        $title = $values[$i]['value'];
    }
    
    ImageTTFText($im, $text_font_size, 0,
        $text_left_shift, $text_top_shift + $vertical_offset, 
        $text_colour, $image_fontfile, $title);
    $vertical_offset += 12;
}

if ($imgData[2] == IMAGETYPE_JPEG) { 
    $imageOutputFunc($im, '', 100);
} else { 
    $imageOutputFunc($im);  // GIF and PNG output funcs take same args
} 

ImageDestroy($im);
?>
 
How did you get so dang smart with this stuff?
 
Awesome :D

The character encoding is great now. Does this support transparent PNGs? I'm giving it a transparent PNG but it outputs a non-transparent PNG.
 
Awesome :D

The character encoding is great now. Does this support transparent PNGs? I'm giving it a transparent PNG but it outputs a non-transparent PNG.

Try this, it looks like it works.

Honestly I am slightly amazed that the definition of the "output" function within the switch/case actually works, but thats the beauty of scripting languages.

PHP:
<?php 
// Image Configuration  
$image_filename     = "background.png";     // Path to background image 
$image_fontfile     = "verdana.ttf";        // Path to font file 
$text_font_size     = 10;                   // Size in Points 
$text_left_shift    = 5;                    // Number of pixels from the left to place the text 
$text_top_shift     = 60;                   // Number of pixels from the top to place the text 
$text_max_chars     = 40;                   // Maximum number of charaters to show before adding '...' 

$text_colour_red    = 64;                   // Text colour red value 
$text_colour_green  = 89;                   // Text colour green value 
$text_colour_blue   = 132;                  // Text colour blue value 

// RSS Configuration 
$rss_feed_url       = "http://domain.com/feed.xml";  
$number_of_entries  = 6;

// Go for code
$parser = xml_parser_create();
xml_parse_into_struct($parser,
    file_get_contents($rss_feed_url), $values, $tag_index); 

$title_tags = array_slice($tag_index['TITLE'], 1, $number_of_entries); 

// Image Creation  
$imgData = getimagesize($image_filename); 
header("Content-type: {$imgData['mime']}"); 

switch($imgData[2]) { 
    case IMAGETYPE_GIF: 
        $im = ImageCreateFromGIF($image_filename);
        
        function output($im) {
            ImageGIF($im);
        }
        
        break; 
    case IMAGETYPE_JPEG: 
        $im = ImageCreateFromJPEG($image_filename);
        
        function output($im) {
            ImageJPEG($im, '', 100);
        }
        
        break; 
    case IMAGETYPE_PNG: 
        $im = ImageCreateFromPNG($image_filename);
        ImageAlphaBlending($im, false);
        
        function output($im) {
            ImageSaveAlpha($im, true);
            ImagePNG($im);
        }
        
        break; 
    default: 
        die ("Unsupported Image Type"); 
} 

$black = ImageColorAllocate($im,0,0,0); 
$text_colour = ImageColorAllocate($im,  
    $text_colour_red, $text_colour_green, $text_colour_blue);  

$vertical_offset = 0; 

foreach ($title_tags as $i) { 
    if (strlen($values[$i]['value']) > $text_max_chars) { 
        $title = substr($values[$i]['value'], 0, $text_max_chars - 5) . "..."; 
    } else { 
        $title = $values[$i]['value']; 
    } 
     
    ImageTTFText($im, $text_font_size, 0, 
        $text_left_shift, $text_top_shift + $vertical_offset,  
        $text_colour, $image_fontfile, $title); 
    $vertical_offset += 12; 
} 

output($im);

ImageDestroy($im);
?>
 
defining a function within a switch/case statement. Just wait till the next version of PHP (6) screws that up when they properly implement scopes. :p
 
Thanks again mate.

Looks like I'm going to learn PHP :D
 
Last edited:
This version uses the create_function method which means it should still work in PHP 6 :D

PHP:
<?php  
// Image Configuration   
$image_filename     = "background.png";     // Path to background image  
$image_fontfile     = "verdana.ttf";        // Path to font file  
$text_font_size     = 10;                   // Size in Points  
$text_left_shift    = 5;                    // Number of pixels from the left to place the text  
$text_top_shift     = 60;                   // Number of pixels from the top to place the text  
$text_max_chars     = 40;                   // Maximum number of charaters to show before adding '...'  

$text_colour_red    = 64;                   // Text colour red value  
$text_colour_green  = 89;                   // Text colour green value  
$text_colour_blue   = 132;                  // Text colour blue value  

// RSS Configuration  
$rss_feed_url       = "http://domain.com/feed.xml";   
$number_of_entries  = 6; 

// Go for code 
$parser = xml_parser_create(); 
xml_parse_into_struct($parser, 
    file_get_contents($rss_feed_url), $values, $tag_index);  

$title_tags = array_slice($tag_index['TITLE'], 1, $number_of_entries);  

// Image Creation   
$imgData = getimagesize($image_filename);  
header("Content-type: {$imgData['mime']}");  

$output = '';
switch($imgData[2]) { 
    case IMAGETYPE_GIF: 
        $im = ImageCreateFromGIF($image_filename);
        $output = create_function('$im', 'ImageGIF($im);');
        break; 
    case IMAGETYPE_JPEG: 
        $im = ImageCreateFromJPEG($image_filename);
        $output = create_function('$im', 'ImageJPEG($im, "", 100);');
        break; 
    case IMAGETYPE_PNG: 
        $im = ImageCreateFromPNG($image_filename);
              ImageAlphaBlending($im, false);
        $output = create_function('$im', 'ImageSaveAlpha($im, true); ImagePNG($im);');
        break; 
    default: 
        die ("Unsupported Image Type"); 
}

$black = ImageColorAllocate($im,0,0,0);  
$text_colour = ImageColorAllocate($im,   
    $text_colour_red, $text_colour_green, $text_colour_blue);   

$vertical_offset = 0;  

foreach ($title_tags as $i) {  
    if (strlen($values[$i]['value']) > $text_max_chars) {  
        $title = substr($values[$i]['value'], 0, $text_max_chars - 5) . "...";  
    } else {  
        $title = $values[$i]['value'];  
    }  
      
    ImageTTFText($im, $text_font_size, 0,  
        $text_left_shift, $text_top_shift + $vertical_offset,   
        $text_colour, $image_fontfile, $title);  
    $vertical_offset += 12;  
}  

$output($im); 

ImageDestroy($im);
?>
 
Stop showing off. But have a cookie anyway.
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,623
Latest member
AndersonLo
Back