Jan.25

Hosting Datafeed Images Locally

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 🙂

How To & Tips
Share this Story:
  • facebook
  • twitter
  • gplus

Comments(14)

  1. Deborah Carney
    2140 days ago

    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
    2138 days ago

    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
    2133 days ago

    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
      2133 days ago

      Wow! I just got schooled!

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

      • Tricia Meyer
        2133 days ago

        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

  4. Julie
    2106 days ago

    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
      2105 days ago

      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
    1817 days ago

    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
      1816 days ago

      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
        1815 days ago

        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
          1815 days ago

          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
            1808 days ago

            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
            1805 days ago

            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
            1805 days ago

            Eric, thank you for sharing your knowledge.

Leave a comment

Comment