• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 742
  • Last Modified:

Show only 1 image in Rss2Html using PHP

Hi, in html2rss PRO
1. The items in my feed usually have many images, but I want to only show the first image.

2. The feed includes full size images, Is there PHP code that can autothubnail the full image to say 125px x 94px ?

Attached is my edited Rss2Html template I am using and it can accept additional PHP to display items.
<html> 
  <head> 
    <meta http-equiv="content-type" content="text/html;charset=utf-8"> 
    <title>~~~FeedTitle~~~</title> 
  </head> 
  <body> 
    <h1>~~~FeedTitle~~~</h1> 
    <p>~~~FeedDescription~~~</p> 
    <hr> 
    <table width="100%"> 
      ~~~BeginItemsRecord~~~ 
<?php 
// Set initial values for our variables 
$theImage = "&nbsp;"; // To ensure proper display with borders 
$newDescription = "~~~ItemDescription~~~"; 
$match = Array(); 

// Use regular expressions to find <img ... > 
if (preg_match("/<img[^>]*>/", $newDescription, $match) !== FALSE) { 
  // Make sure it truely was a match 
  if ((count($match) != 0) && ($match[0] != "")) { 
    // Save the image 
    $theImage = $match[0]; 
    // Remove the image from the description 
    $newDescription = str_replace($match[0], "", $newDescription); 
  } 
} 

echo "<tr><td>$theImage</td><td>~~~ItemPubShortDate~~~ ~~~ItemPubShortTime~~~<br>"; 
echo "~~~ItemTitle~~~<br>"; 
echo "$newDescription</td></tr>"; 
echo "<tr><td colspan=\"2\"><hr></td></tr>"; 
?> 
      ~~~EndItemsRecord~~~ 
    </table> 
  </body> 
</html>

Open in new window

0
greenerpastures
Asked:
greenerpastures
  • 5
  • 4
  • 2
2 Solutions
 
Blue-hitCommented:
about 1: What exactly is the content of $theImage right now?

about 2: You''ll need an additional prgramm for image manipulaion installed on xour web serer (for example ImageMagick) the php code for resizing will depend on that software.
0
 
greenerpasturesAuthor Commented:
RE: about 1:
Here is the sample Feed item the template above produces:

<?xml version="1.0"?>
<rss version="2.0" xmlns:ps="">
<channel>
<title>Listings RSS 2.0 Feed</title>
<link>site.com/trade/housetrading.xml</link>
<description>A RSS 2.0 data feed used by Propsmart to add properties.</description>
      <item>
      <title> listing title </title>
         <description><![CDATA[<a href="site.com/listing-10"><img src="site.com/trade/images/listing_photos/10_1080streetleft.jpg" width="50px"/>
<img src="site.com/trade/images/listing_photos/10_flaglerestates3d.jpg" width="50px"/>
<img src="site.com/trade/images/listing_photos/10_989allisonaerial.jpg" width="50px"/> View listings</a> bla, bla, bla]]></description>  
      </item>  
</channel>
</rss>
0
 
Ray PaseurCommented:
A strategy to show only the first image would be to create an array of image names.  Test to see if the image name is in the array.  If it is, do not display the image.  In any case, add the image name to the array.

A strategy to resize the image would include adding image-resizing code to the PHP code above.  That is a fairly advanced topic.  I can show you this much about how it is done.  Hopefully you can adapt that to your application needs.

best regards for the new year, ~Ray
<?php // RAY_image_resize.php RESIZE IT TO FIT A PREDEFINED SIZE


// A FUNCTION TO DETERMINE IF GD IS AT LEVEL 2 OR MORE
function get_gd_info($display=FALSE)
{

// IS GD INSTALLED AT ALL?
   if (!function_exists("gd_info"))
   {
      if ($display) echo "<br/>GD NOT INSTALLED\n";
      return FALSE;
   }

// IF GD IS INSTALLED GET DETAILS
   $gd = gd_info();

// IF DISPLAY IS REQUESTED, PRINT DETAILS
   if ($display)
   {
      echo "<br/>GD DETAILS:\n";
      foreach ($gd as $key => $value)
      {
         if ($value === TRUE)  $value = 'YES';
         if ($value === FALSE) $value = 'NO';
         echo "<br/>$key = $value \n";
      }
   }

// RETURN THE VERSION NUMBER
   $gd_version = ereg_replace('[^0-9\.]', '', $gd["GD Version"]);;
   return $gd_version;
}


// A FUNCTION TO MAKE AN IMAGE INTO THE RIGHT WIDTH FOR PAGE DISPLAY
// WILL WORK IF GD2 NOT INSTALLED, BUT WILL MAKE BETTER IMAGES WITH GD2
// INPUT IS THE IMAGE FILE NAME, OUTPUT IS AN IMAGE RESOURCE, OR FALSE IF NO RESIZE NEEDED
function create_right_size_image($image, $width=300)
{
// IS GD HERE?
   $gdv = get_gd_info();
   if (!$gdv) return FALSE;

// GET AN IMAGE THING
   $source = imagecreatefromjpeg("$image");

// GET THE X AND Y DIMENSIONS
   $imageX = imagesx($source);
   $imageY = imagesy($source);

// IF NO RESIZING IS NEEDED
   if ($imageX <= $width)
   {
      return FALSE;
   }

// THE WIDTH IS TOO GREAT - MUST RESIZE
   $tnailX = $width;
   $tnailY = (int) (($tnailX * $imageY) / $imageX );

// WHICH FUNCTIONS CAN RESIZE / RESAMPLE THE IMAGE?
   if ($gdv >= 2)
   {
// IF GD IS AT LEVEL 2 OR ABOVE
      $target = imagecreatetruecolor($tnailX, $tnailY);
      imagecopyresampled ($target, $source, 0, 0, 0, 0, $tnailX, $tnailY, $imageX, $imageY);
   } else
   {
// IF GD IS AT A LOWER REVISION LEVEL
      $target = imagecreate($tnailX, $tnailY);
      imagecopyresized   ($target, $source, 0, 0, 0, 0, $tnailX, $tnailY, $imageX, $imageY);
   }
   return $target;
}


// SAMPLE CALLING SEQUENCE
// RESIZE THE FILE TO FIT PAGE WIDTH, IF NECESSARY
if ($imageblob = create_right_size_image($my_image_file))
{
    // CREATE YOUR NEW FILE NAME HERE?
    $my_new_image_file = 'YOUR FILE NAME';
    
    // STORES THE IMAGE IN A NEW FILE NAME
    imagejpeg($imageblob, $my_new_image_file);
}

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
greenerpasturesAuthor Commented:
As to #1, I just realized that thumbs are already stored on the server,
and all I have to do is to add "/thumb_" in front of the image file name:

Full image; site.com/trade/images/listing_photos/10_1080streetleft.jpg

Thumb: site.com/trade/images/listing_photos/thumb_10_1080streetleft.jpg

For some reason this code does not replace images with thumbs:

$digit = preg_match("/^[0-9]+$/",$theImage);
$theImage = preg_replace($digit,"thumb_".$digit);
0
 
Ray PaseurCommented:
You do not need REGEX - it unnecessarily complicates a simple character-string replacement.  Try this strategy instead...
// REPLACE WITH PATH TO THUMBS
$url = 'site.com/trade/images/listing_photos/10_1080streetleft.jpg';
$new = (str_replace('listing_photos/', 'listing_photos/thumb_', $url);
echo $new;

Open in new window

0
 
greenerpasturesAuthor Commented:
Great, After removing the first "(" your code now works, so the second problem is resolved gracefully!
$new = str_replace('listing_photos/', 'listing_photos/thumb_', $url);

As to problem #1 above, can someone suggest an alternative route to only show the first image in a rss feed? Each item can have from 0 to 20 images with unlimited name combinations.

As such, Ray's suggestion above is hard to implement:

"A strategy to show only the first image would be to create an array of image names.  Test to see if the image name is in the array.  If it is, do not display the image.  In any case, add the image name to the array."
0
 
Blue-hitCommented:
The key is to devide the XML into it's elements to put it in an array. That CAN be done with pure php, but it would be much more easy if you used a framework that supports such actions. AI'd suggest the usage of Zend Framework, even if you need some basic knowledge in php and oo programming. There are plenty of good tutorials around ;-)

For an istance the follwoing code should do:


<?php 
$XMLResponce = '<?xml version="1.0"?>
<rss version="2.0" xmlns:ps="">
<channel>
<title>Listings RSS 2.0 Feed</title>
<link>site.com/trade/housetrading.xml</link>
<description>A RSS 2.0 data feed used by Propsmart to add properties.</description>
      <item>
      <title> listing title </title>
         <description><![CDATA[<a href="site.com/listing-10"><img src="site.com/trade/images/listing_photos/10_1080streetleft.jpg" width="50px"/>
<img src="site.com/trade/images/listing_photos/10_flaglerestates3d.jpg" width="50px"/>
<img src="site.com/trade/images/listing_photos/10_989allisonaerial.jpg" width="50px"/> View listings</a> bla, bla, bla]]></description>  
      </item>  
</channel>
</rss>';

$a_tags = split('<',$XMLResponce);
$a_imagetags = array();
foreach ($a_tags as $tag){
	if(stristr($tag,'img')){
		array_push ( $a_imagetags, $tag );
	}
}
$a_imagearray = array();
foreach ($a_imagetags as $imagetag){
	$foundImage = "";
	$start = strpos($imagetag,'src="')+5;
	$end = strpos($imagetag,'"',$start);
	$foundImage = substr ( $imagetag, $start , $end-$start );		
	if($foundImage!= ""){
		array_push ( $a_imagearray, $foundImage );
	}		
}

foreach ($a_imagearray as $image){
	echo($image);
	echo("<br>");					
}
?>

Open in new window

0
 
greenerpasturesAuthor Commented:
Thanks Blue,
I tried yours, could not get it to work with my RSS setup (I am sure my mistake somewhere). But I did figure out this code which now removes all remaining images from the above example:

 $newDescription = preg_replace("/(<img[^>]*>)/", "", $newDescription);
0
 
greenerpasturesAuthor Commented:
Thanks to everyone!
0
 
Ray PaseurCommented:
"the first image in a rss feed?" - this is kind of an oxymoron - XML does not have a natural order to it - it appears in the order it was generated.  But if you trust the data source to create the XML in a predictable and useful sequence, you might consider using SimpleXML and an iterator to work over the RSS.
0
 
Ray PaseurCommented:
Thanks for the points - it is an interesting question, for sure!  ~Ray
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now