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

Discussion in 'Web Design & Coding' started by melon, Jan 24, 2005.

  1. melon

    melon MS-DOS 2.0 Political User

    Messages:
    854
    Location:
    Ásgarðr
    I have an RSS feed for my blog, and I was wondering if there's a script out there to feed RSS into a signature image?

    Just like NetRyder's signature here:

    [​IMG]

    I know it exists, but don't know where to find it!

    Melon
     
  2. NetRyder

    NetRyder Tech Junkie Folding Team

    Messages:
    13,256
    Location:
    New York City
    I was initially using the FeedBurner headline animator which is really easy to setup and use.
    http://www.feedburner.com/fb/a/aboutgiffy;jsessionid=3ACFB6DD66605FBAC3774F3D5998ADC0.app3

    The new set that you see, I coded from scratch, mainly as a learning experience. Plus I could customize it the way I wanted. If you've used the GD library before with PHP, it shouldn't be that hard to do. Parse the XML file to pull out only the post titles and stick them onto the image using imagettftext()

    I'm sure there are ready scripts somewhere out there, but I'd suggest trying it on your own first. It's more fun that way. Look up some GD tutorials for reference. I can also help you along the way if you need anything. :)
     
  3. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    I think I did one for here which used the PHP4 SAX extension to get the RSS data and then GD to overlay text on an image. ElectronicPunk should have it around here somewhere I think
     
  4. melon

    melon MS-DOS 2.0 Political User

    Messages:
    854
    Location:
    Ásgarðr
    I will first admit that I'm new at PHP, but I'm slowly working my way through it. My first experience with it, so far, has been implementing existing PHP-based pages, like web forums and blogs. But I know it isn't something that's too difficult to figure out, as I've done other programming in the past.

    Well, it looks like the GD library is really not that difficult, so that's a good first step. I guess where I need help now is in parsing the RSS feed, so it can be used for imagettftext(). Also, if possible, is there a function that allows you to hyperlink the headlines? That would be great, if possible.

    Thanks for your help...

    Melon
     
  5. melon

    melon MS-DOS 2.0 Political User

    Messages:
    854
    Location:
    Ásgarðr
    http://www.xeidon.org/test/index5.php

    Alright...here's my code:

    PHP:
    <?php

    class RSSParser {

        var 
    $insideitem false;
        var 
    $tag "";
        var 
    $title "";
        var 
    $link "";

        function 
    startElement($parser$tagName$attrs) {
            if (
    $this->insideitem) {
                
    $this->tag $tagName;
            } elseif (
    $tagName == "ITEM") {
                
    $this->insideitem true;
            }
        }

        function 
    endElement($parser$tagName) {
            if (
    $tagName == "ITEM") {
                
    printf("<dt><b><a href='%s'>%s</a></b></dt>",
                    
    trim($this->link),htmlspecialchars(trim($this->title)));
                
    $this->title "";
                
    $this->link "";
                
    $this->insideitem false;
            }
        }

        function 
    characterData($parser$data) {
            if (
    $this->insideitem) {
            switch (
    $this->tag) {
                case 
    "TITLE":
                
    $this->title .= $data;
                break;
                case 
    "LINK":
                
    $this->link .= $data;
                break;
            }
            }
        }
    }

    $xml_parser xml_parser_create();
    $rss_parser = new RSSParser();
    xml_set_object($xml_parser,&$rss_parser);
    xml_set_element_handler($xml_parser"startElement""endElement");
    xml_set_character_data_handler($xml_parser"characterData");
    $fp fopen("http://www.xeidon.org/feed/rss2/","r")
        or die(
    "Error reading RSS data.");
    while (
    $data fread($fp4096))
        
    xml_parse($xml_parser$datafeof($fp))
            or die(
    sprintf("XML error: %s at line %d"
                
    xml_error_string(xml_get_error_code($xml_parser)), 
                
    xml_get_current_line_number($xml_parser)));
    fclose($fp);
    xml_parser_free($xml_parser);

    ?>
    What I want to do is fix it so I only have the last four headlines. Anyone have any ideas? As it stands, it parses everything in the feed. After I get this part figured out, I'm going to work on applying it to GC library.

    Also, if you click on the first link, things like periods and quotation marks turn into unsightly "?"s, even though the RSS feed does not do that. Have any suggestions on how the page should be character encoded (e.g., Unicode UTF-8, etc.)?

    Melon
     
  6. a1ehouse

    a1ehouse Chamone M*tha Fu*ka

    Messages:
    288
    You need to append the while statement, or add a new if statement, to include a counter.
    http://www.w3schools.com/php/php_looping.asp


    PHP:
    <html>
    <body>
    <?php
    for ($i=1$i<=4$i++)
    {echo 
    "Hello World!<br />";
    }
    ?>
    </body>
    </html>
     
  7. melon

    melon MS-DOS 2.0 Political User

    Messages:
    854
    Location:
    Ásgarðr
    I feel like an idiot right now. For the life of me, I can't figure out where to put the "looping" statement to make it work. All I get are errors.

    Any help? :p

    Melon
     
  8. melon

    melon MS-DOS 2.0 Political User

    Messages:
    854
    Location:
    Ásgarðr
    Nevermind. I started to wonder if, in Wordpress, there was a way to limit how many articles it syndicates to RSS. And there is! I have it now set to 5 (I changed my mind on 4). Thus, I don't need to worry about looping statements.

    Thanks for your help on all this.

    Melon
     
  9. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    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;
    $maxlen60;

    // RDF Parsing Vars
    $url   "http://www.osnn.net/modules.php?modname=backend&action=rdf";
    $temp  "temp.xml";
    $store 0;
    $count 0;
    $maxent4;
    $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 == && $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($tempfile_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($handlefilesize($temp))) {
        if (!
    xml_parse($parser$datafeof($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($value045) . "..";
        }
    }

    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);
    ?> 
     
    melon likes this.
  10. melon

    melon MS-DOS 2.0 Political User

    Messages:
    854
    Location:
    Ásgarðr
    Ahh...great! Much better than my last code and very easy to customize.

    My new signature is now a product of that code, plus a few customizations I made, as a result. Much thanks!

    Melon
     
  11. NetRyder

    NetRyder Tech Junkie Folding Team

    Messages:
    13,256
    Location:
    New York City
    Nicely done, Geffy and melon. Looks great :)
     
  12. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    wow looking very nice there, glad that code helped
     
  13. ZenithalRavage

    ZenithalRavage OSNN One Post Wonder

    Messages:
    6
    Location:
    The Netherlands
    Hello guys! :)

    I've been trying to make a sig like this but without success. I have tried your code Geffy but the image does not display (red cross in Internet Explorer).

    Anybody know what I'm doing wrong?

    http://www.a-base.dds.nl/sig_rss/rss_image.php

    EDIT:
    hmm I tried a dynamic signature tutorial thingy and did everything by the book and again I run into the same problem!

    IE shows red cross, Firefox tells me:
    The image "http://www.a-base.dds.nl/sigtest/sample_signature.png" cannot be displayed, because it contains errors.

    It makes me think it doesn't parse it correctly as an image or something, I can't include a .htaccess file because my provider doesn't support those, any idea what to do?

    EDIT2:
    hey I can get the .png (second) sig to work just by renaming it to .php so that it is parsed correctly:
    http://www.a-base.dds.nl/sigtest/random_images_signature.php
    Does this mean that my problem with the first sig is some totally different problem as the file allready has the .php extension?
     
    Last edited: May 6, 2005
  14. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    can you make copies of the two php files and give them the extension .phps so that we can look at the php source code. It would be easier to trace the problem that way.
     
  15. ZenithalRavage

    ZenithalRavage OSNN One Post Wonder

    Messages:
    6
    Location:
    The Netherlands
    Last edited: May 6, 2005
  16. ZenithalRavage

    ZenithalRavage OSNN One Post Wonder

    Messages:
    6
    Location:
    The Netherlands
    I added this to report error's:
    error_reporting(E_ALL);

    This showed me error's on $count and $maxent

    then I changed this:

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


    into this:

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


    and those error's disappeared, but still no luck:crosseyed:


    http://www.a-base.dds.nl/sig_rss/rss_image_b.php
    http://www.a-base.dds.nl/sig_rss/rss_image_b.txt <-code


    EDIT:
    I think the problem is with my host because I tested the standard example code the PHP manual gives for the imagettftext function and that gives me the same error!
    http://www.a-base.dds.nl/sig_rss/test.php
    http://www.a-base.dds.nl/sig_rss/test.txt <-code

    I emailed my host about this, I'll keep you all informed :)
     
    Last edited: May 7, 2005
  17. ZenithalRavage

    ZenithalRavage OSNN One Post Wonder

    Messages:
    6
    Location:
    The Netherlands
    I've narrowed the problem down to the imagettftext thing, seems my host's server doesn't understand it, haven't heard anything from my host yet though...

    I got this thing to work with the imagestring function in the meantime :)

    How would one go to get the links in there aswell?

    http://www.a-base.dds.nl/sig_rss/rss_image_d.php
    http://www.a-base.dds.nl/sig_rss/rss_image_d.txt <-code


    EDIT:
    Hey this imagestring ain't half bad after a bit of cosmetics:

    http://www.a-base.dds.nl/sig_rss/zr_sig_rss.php

    whoohoo :D

    Now how to get description in there? ah well, I'm pretty happy so far ;)
     
    Last edited: May 7, 2005
  18. Grandmaster

    Grandmaster Electronica Addict Political User Folding Team

    Messages:
    10,574
    Location:
    Santa Clara, CA
    Would there be a way to individually hyperlink each post/entry in the sig? I'd be interested then :p
     
  19. ZenithalRavage

    ZenithalRavage OSNN One Post Wonder

    Messages:
    6
    Location:
    The Netherlands
    That's odd, I found out my image doesn't totally load on Internet Explorer, on Firefox no probs, I know IE sucks and nobody should use it but what the heck could be causing this because the other sigs I see on this page load normally :crosseyed:
     
  20. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    it should work in IE, make sure you completely refresh your cache.

    the imagettftext thing would probably be caused by your host not having FreeType2 support in the GD extension for PHP.