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:


	$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");

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

	header('Content-Type: text/xml');
?><<?= '?' ?>xml version="1.0" encoding="UTF-8"?>
 <urlset xmlns=""

$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)) {
		<loc><?= simplify($rsData['Name']) ?>-p<?= $rsData['ProductID'] ?>.php</loc>
			<image:loc><?= simplify($rsData['Name']) ?>-i<?= $rsData['ProductID'] ?>.jpg</image:loc>
			<image:caption><?= $rsData['Name'] ?></image:caption>
} // ends while ($rsData = mysql_fetch_array($oResult))


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 , 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.

6 Comments » for Creating an Image Sitemap from a Datafeed
  1. Panda says:

    I got an error when running images.php

    XML Parsing Error: no element found
    Line Number 7, Column 1:

  2. Eric Nagel says:

    @Panda – contact me w/ your actual domain & error. Looks like an XML error, not PHP

  3. Panda says:

    Well, it’s the tea site,, and mostly your code. I just added two fields to line 9.

    $aProductsToInclude = array(“Green Tea”, “White Tea”, “Black Tea”, “Oolong Tea”, “Iced Tea”,”Tea Cups”,”Tea Pots”);

  4. Panda says:

    Could it be i was saving the file as ANSI instead of UTF-8 ?? I just realized my notepad++ was set to ANSI. Now, should I use UTF-8 with or without byte order mark?

  5. Linda says:

    Whats the vars.php file?
    And in the code of type of tea need to change depending of the kind of product a site have right?


1 Pings/Trackbacks for "Creating an Image Sitemap from a Datafeed"
  1. […] <plug type=”shameless”>Creating an Image Sitemap from a Datafeed</plug> […]

Leave a Reply

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