PHP/GD + RSS

PHP/GD + RSS - PHP - Programmation

Marsh Posté le 13-05-2008 à 14:37:16    

Bonjour,
 
Je commence à m'autoformer sur les biblio GD. J'ai récupéré ce bout de code, qui permet de faire un image à partir d'un flux rss :
 

Code :
  1. <?php
  2.   $url = "http://www.kerweb.fr/wp-rss.php"; //Should be a full URL, not a local reference.
  3.   $displayname = "Kerweb.fr";
  4.   $middle = "dernierement :";
  5.   $rssHeadline = 0; //Which headline to display. 0 is the latest, 1 is the 1st one before the latest, 2 is the 2nd before latest, etc
  6.   $filename = "sig.jpg"; //File name to save the image as
  7.   $minutes = 5; //How often to update the image. 0 indicates that image updates every time it is called (heavy strain on bandwidth & server)
  8.   $forcewidth = 0; //If set to 0, image will be as long as it needs to be. If set to anything else, image will be set at a forced width. Any text that doesn't fit will be cut off
  9.   //In-URL definitions. Allows you to customize the image while calling it. If you're worried that others may
  10.   //be exploiting your server to run their own image, remove the following chunk or comment it. Also note, when
  11.   //using an in-URL definition, image file will not be created and the RSS file will be querried each time. This
  12.   //should only be used wisely.
  13.   if ($_GET['url']) { $url = $_GET['url']; $minutes = 0; $save = FALSE;}
  14.   if ($_GET['displayname']) { $displayname = $_GET['displayname']; $minutes = 0; $save = FALSE;}
  15.   if ($_GET['middle']) { $middle = $_GET['middle']; $minutes = 0; $save = FALSE;}
  16.   if ($_GET['rssHeadline']) { $rssHeadline = $_GET['rssHeadline']; $minutes = 0; $save = FALSE;}
  17.   if ($_GET['forcewidth']) { $forcewidth = $_GET['forcewidth']; $minutes = 0; $save = FALSE;}
  18.   //If file's modify time is too recent, just load the image instead of creating it again
  19.   if ($minutes > 0 && is_file($filename) && ((time()-filemtime($filename)) < ($minutes * 60))) {
  20.    header("Content-type: image/jpeg" );
  21. $im = imagecreatefromjpeg($filename);
  22.    imagejpeg($im,"",100);
  23. die;
  24.   }
  25.  
  26.   $rdfHandle = fopen($url,"r" ) ; // Open the RDF file for reading  
  27.   $rdfData = fread($rdfHandle,10000) ; // Read in the RDF data.
  28.   $rdfData = chop($rdfData); // Strip any whitespace from the end of the string     
  29.   $rdfData = ereg_replace("[\r,\n]", "", $rdfData); //Clear line breaks
  30.   $rdfData = strstr($rdfData,"<item" ); //Remove everything before <item>.
  31.   $rdfData = str_replace("<item>","", $rdfData); //Remove <item> itself
  32.   $rdfData = urldecode($rdfData); //Replace any silly %20-type characters with their readable replacement.
  33.   $rdfArray = explode("</item>",$rdfData); //Creates an Array from all the headlines
  34.   $title = ereg_replace("<title>","",$rdfArray[$rssHeadline]); // Remove the leading <title> tags from the selected headline
  35.   $title = str_replace(strstr($title,"</title>" ),"",$title); //  Remove </title> and anything after it
  36.   $title = trim($title);
  37.   // create the image with appropriate dimensions
  38.   $width = ((strlen($title) + strlen($middle) + strlen($displayname)) * ImageFontWidth(2)) + 10;
  39.   if ($forcewidth > 0) { $width = $forcewidth;}
  40.   $im = imagecreate($width, 15);
  41.   $bg = imagecolorallocate($im, 255, 255, 255); //Color of background; currently white
  42.   $textcolor = imagecolorallocate($im, 0, 0, 0); //Color of text; currently black
  43.   $middlecolor = imagecolorallocate($im, 128, 128, 128);//Color of $middle text; currently gray
  44.   // Write the string onto the image then save it
  45.   imagestring($im, 2, $width - (strlen($title) * 6) - (strlen($middle) * 6) - 4, 1, $middle, $middlecolor);
  46.   imagestring($im, 2, $width - (strlen($title) * 6), 1, $title, $textcolor);
  47.   imagestring($im, 2, 2, 1, $displayname, $textcolor);
  48.   if ($save != FALSE) { imagejpeg($im,$filename,100); }//Save file
  49.   // output the image
  50.   header("Content-type: image/jpeg" );
  51.   imagejpeg($im,"",100);
  52. ?>


 
Et voilà ce que ça donne :
 
http://www.gregorythepault.fr/documents/tests/rss/rssimg.php
 
Est-ce que vous voyez pourquoi je n'arrive pas à récupérer ce flux rss (qui fonctionne pourtant bien chez moi) ?
 
Merci d'avance à ceux qui pourront m'aiguiller. :hello:

Reply

Marsh Posté le 13-05-2008 à 14:37:16   

Reply

Marsh Posté le 14-05-2008 à 16:34:46    

C'est quoi ce parsing xml  :ouch:  
A mon avis, avant de vouloir afficher ton feed rss dans une image, essai déjà de l'afficher simplement en texte en refaisant correctement ton parsing xml.
Pour cela tu peux te servir de ca : http://fr.php.net/manual/en/book.dom.php

Reply

Marsh Posté le 15-05-2008 à 00:31:23    

J'étais parti de la mauvaise méthode, en essayant de reprendre un code existant...
 
J'ai maintenant réussi à bien récupérer mon flux rss, je vais reprendre à 0 pour en faire une image clean.
 
Si quelqu'un aurait un site clair sur la façon de procéder je suis preneur  :)

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed