Eric Nagel

Eric Nagel

CTO, PHP Programmer, Affiliate Marketer & IT Consultant

Hosting Datafeed Images Locally

I have been, or can be if you click on a link and make a purchase, compensated via a cash payment, gift, or something else of value for writing this post. Regardless, I only recommend products or services I use personally and believe will be good for my readers.

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:

mysql_query($cQuery);
if (mysql_error()) {
	echo("<p><b>MySQL Error: " . mysql_error() . "<br />\n");
	echo("$cQuery</p>");
	echo("<pre>");
	print_r($rsItem);
	echo("</pre>");
	break;
} // 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;

		`$cCommand`;
	} // 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 {
	echo($rsData['Thumbnail']);
} // 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 🙂

Comments
  • Deborah Carney
    Posted January 25, 2011 8:04 pm 0Likes

    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.

  • Gail Gardner
    Posted January 27, 2011 10:30 pm 0Likes

    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.

  • Kevin Meyer
    Posted February 1, 2011 3:54 pm 0Likes

    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.

    Cheers!

    • Eric Nagel
      Posted February 1, 2011 4:44 pm 0Likes

      Wow! I just got schooled!

      Yeah, there are definitely more elegant ways to handle this problem. I took the short-cut.

      • Tricia Meyer
        Posted February 1, 2011 5:08 pm 0Likes

        And my husband gets to make his first public appearance ever in affiliate marketing. 🙂 If not for Eric, he would still be living in the shadow.s

  • Julie
    Posted February 28, 2011 7:45 pm 0Likes

    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
      Posted March 1, 2011 10:24 am 0Likes

      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.

  • SAm
    Posted December 14, 2011 7:10 pm 0Likes

    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
      Posted December 15, 2011 8:30 am 0Likes

      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
        Posted December 16, 2011 1:08 pm 0Likes

        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
          Posted December 16, 2011 1:30 pm 0Likes

          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…
          http://ericnagel.wpengine.com/php-programmer

          • Jon
            Posted December 24, 2011 12:06 am 0Likes

            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
            Posted December 26, 2011 10:30 am 0Likes

            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
            Posted December 26, 2011 8:03 pm 0Likes

            Eric, thank you for sharing your knowledge.

Leave A Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.