Eric Nagel

Creating an Image Sitemap from a Datafeed

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.

Google just announced an image sitemap format which will help get images from your site indexed. I thought this was a perfect extension to the datafeed series.

The first step is to make Google think we have the images on our server. So inside an “images” folder, create “image.php” like so:

<?php
	include('../vars.php');

	$cQuery = "select * from products where ProductID=" . (int)$_GET['ProductID'] . " limit 1";
	$oResult = mysql_query($cQuery);
	$rsData = mysql_fetch_array($oResult);

	header('Content-Type: image/jpeg');
	$fp = fopen($rsData['Thumbnail'], "r");
	fpassthru($fp);
	fclose($fp);
	exit();
?>

next, at the root of the site, create images.php:

<?php
	header('Content-Type: text/xml');
	include("./vars.php");
?><<?= '?' ?>xml version="1.0" encoding="UTF-8"?>
 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
  xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<?php

$aProductsToInclude = array("Green Tea", "White Tea", "Black Tea", "Oolong Tea", "Iced Tea");

$cQuery = "select * from products where (";
foreach ($aProductsToInclude as $cCategory) {
	$cQuery .= "MerchantSubcategory like '%" . $cCategory . "%' or MerchantCategory like '%" . $cCategory . "%' or ";
} // ends foreach ($aProductsToInclude as $cCategory)
$cQuery = ereg_replace(" or $", ")", $cQuery);
$cQuery .= " and Thumbnail<>'' order by Name";
// echo("$cQuery");
$oResult = mysql_query($cQuery);
while ($rsData = mysql_fetch_array($oResult)) {
	?>
	<url>
		<loc>http://www.greenwhiteandblacktea.com/<?= simplify($rsData['Name']) ?>-p<?= $rsData['ProductID'] ?>.php</loc>
		<image:image>
			<image:loc>http://www.greenwhiteandblacktea.com/images/<?= simplify($rsData['Name']) ?>-i<?= $rsData['ProductID'] ?>.jpg</image:loc>
			<image:caption><?= $rsData['Name'] ?></image:caption>
		</image:image>
	</url>
	<?php
} // ends while ($rsData = mysql_fetch_array($oResult))

?>
</urlset>

What this does is creates an image sitemap file.

Finally, add to your .htaccess:

RewriteRule ^images/(.*)\-i([0-9]+).jpg$ images/image.php?ProductID=$2 [L]
RewriteRule ^images.xml$ images.php [QSA]

Now, you have an image sitemap (images.xml) and when you visit an image URL like http://www.greenwhiteandblacktea.com/images/Bao-Zhong-Royale-Oolong-i469648571.jpg , htaccess gets the productID, using fopen opens the image, and passes it through to the browser.

If you were slick, you’d add some error checking and caching to these scripts to make things go quicker.

The tea niche may not be ideal for someone searching for images (think shoes!) but this at least gives you an idea of how to take advantage of this new tool by Google.