I was asked today how to host datafeed images on your local server. Starting with the script given in Step 2 of the datafeed series, we’re just going to add some code after the MySQL query has been executed without errors:

if (mysql_error()) {
	echo("<p><b>MySQL Error: " . mysql_error() . "<br />\n");
} // ends if (mysql_error())
else {
	$cThumbnail = $rsItem[$rsMap['Thumbnail']];
	if (!empty($cThumbnail)) {
		// http://feeds2.yourstorewizards.com/3365/images/100x500/1992-vintage-pu-er-tea.jpg

		// Get the image extension
		$aTemp = explode('.', $cThumbnail);
		$cExt = array_pop($aTemp);

		// Make this image name
		$cDestination = $rsItem[$rsMap['MerchantID']] . '-' . $rsItem[$rsMap['ProductID']] . '.' . $cExt;

		$cCommand = '/usr/bin/wget ' . $cThumbnail . ' --output-document=' . $baseDir . 'images/products/' . $cDestination;

	} // ends if (!empty($cThumbnail))
} // ends else from if (mysql_error())

What this does is, looks for a thumbnail image, gets the image file extension (jpg, png, gif), makes a new filename (I based it on the merchantID and productID) then gets the file and writes it back to your server. I’m using wget, because it’s easy to do, and the backtick operator because, again, it’s easy to use.

Then, edit browse.php and make a change to displaying the image:

echo("\t<td width=\"" . round(100/$nCols, 0) . "%\" align=\"center\"><a href=\"/" . simplify($rsData['Name']) . "-p" . $rsData['ProductID'] . ".php\"><img src=\"");

// Does the local file exist?
$aTemp = explode('.', $rsData['Thumbnail']);
$cExt = array_pop($aTemp);
// Make this image name
$cDestination = $rsData['MerchantID'] . '-' . $rsData['ProductID'] . '.' . $cExt;

if (is_file('images/products/' . $cDestination)) {
	echo("/images/products/" . $cDestination);
} // ends if (is_file('images/products/' . $cDestination))
else {
} // ends else from if (is_file('images/products/' . $cDestination))
echo("\" style=\"max-width: 125px; max-height: 125px;\" onerror=\"ImgError(this)\" /><br />" . htmlentities($rsData['Name']) . "</a></td>\n");

Same idea, only this time we’re displaying the image, rather than fetching it. If you’re using the fullsize (big) image, too, you’ll need to duplicate the fetching of the images.

You can see this working on my Green White and Black Tea site.

If you really wanted to get fancy, make the image name keyword-rich 🙂

14 Comments » for Hosting Datafeed Images Locally
  1. This is a great guide! One thing to check for though is whether the merchant terms exclude hosting your own images. Some change images frequently or have contracts with manufacturers that don’t allow the images to be hosted anywhere except the merchant’s site.

    That said, this is a great solution especially for Yahoo stores merchants where the image locations can be changed at Yahoo’s whim without anyone (including the merchant) being informed. There are other solutions for the Yahoo problem, but many Yahoo stores don’t know about it or don’t want to pay extra for the “fix” – which is from a 3rd party.

  2. Gail Gardner says:

    Many merchants and affiliates may not be aware of the issues with Yahoo that Deborah mentioned. I did comprehensive research on that subject and published it at the link used in this comment. Yahoo store expert Rob Snell who wrote Yahoo Stores for Dummies and is one of the two very best on Yahoo stores that I know of provided input as did YourStoreWizards which is another solution for merchants to consider.

    It is CRITICAL for affiliates to know whether a merchant has the disappearing images issue and for merchants to know that if they do not host their images using static links their sales from every feed including affiliate sites and shopping comparison sites including Google Base aka Google Product Search are going to be only a fraction of what they could be.

    I have had affiliates tell me they avoid promoting Yahoo Stores because of low conversions and I believe this issue is the cause. Affiliates should not avoid those Yahoo merchants who have an image hosting solution in place because well-designed Yahoo stores can convert very well.

  3. Kevin Meyer says:

    Hey Eric – instead of punting the image download to the OS with wget, have you ever tried this:

    $url = $cThumbnail;
    $img = “images/” . $cDestination;
    file_put_contents($img, file_get_contents(urlencode($url)));

    you could then catch 404 errors or file creation errors locally and handle. WGET will write a 0 byte file if the source doesn’t exist. That will them make your display is_file condition true and not display a file.


  4. Julie says:

    Great Site! How would I incorporate Coupon Codes into the datafeed? Would they be my product, or would I have to access a different SQL Database?

    Oh yeah, and I can’t get the .css to work with php. Is that just me?

    • Eric Nagel says:

      Hi Julie,

      Coupons require a little more work. First, assume you have something simple (5% off, site-wide, no minimum). What I would do is strike out the price and show the coupon price and text, “with coupon”. Link the price and the “with coupon” and when the user clicks, give them the coupon & redirect them to the deeplinked page.

      If the coupon is something like $10 off $100, you’d have some work to do, but it’s all possible. Coupons + datafeed are a great way to generate sales.

  5. SAm says:

    Thank you Eric
    I’ve followed your tutorials every thing was fine till i added this code to host images locally then ‘admin-sas-datafeed.php’ stop responding the data was inserted in the db_table but no images in products folder. I wonder if i did something wrong?

    • Eric Nagel says:

      Try Kevin’s idea (comment above). Mine uses Linux tools, his sticks with PHP functions, which is more portable & will work in more environments.

      • SAm says:

        Hi Eric
        Thanks for your reply. I have tried Kevin’s idea but i’m still stuck if you can post the whole code i appreciate that or if you can email it to me that would be nice. Thank you again

        • Eric Nagel says:

          I’ve posted the whole code. There’s nothing more… start putting in echo()s and print_r()s to find out where it’s not working.

          I give away 98% of the code, but you’ll have to assemble the rest to make it work in your situation. If you can’t…

          • Jon says:

            Hi Eric,
            Once again excellent tutorial. 😛 I am getting the following error
            Fatal error: Call to undefined function simplify() in /home/johnny01/public_html/mysite/browse.php on line 17
            I fixed it after replacing : simplify($rsData[‘Name’])
            with $rsData[‘Name’]
            I am just curious, what does this function does?

          • Eric Nagel says:

            Simplify() takes something like: “Complex Product Title #2”
            and makes it: “complex-product-title-2”

            $cString = str_replace(“‘”, ”, $cString);
            $cString = preg_replace(“/[^A-Za-z0-9]/”, “-“, $cString);

          • Jon says:

            Eric, thank you for sharing your knowledge.

Leave a Reply

Your email address will not be published. Required fields are marked *