Creating an Image Sitemap from a Datafeed

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="http://www.sitemaps.org/schemas/sitemap/0.9"

$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>http://www.greenwhiteandblacktea.com/<?= simplify($rsData['Name']) ?>-p<?= $rsData['ProductID'] ?>.php</loc>
			<image:loc>http://www.greenwhiteandblacktea.com/images/<?= 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 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.

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


  1. Trackback: 9021-SEO & the Unknown Pixel

  2. Panda
    3095 days ago

    I got an error when running images.php

    XML Parsing Error: no element found
    Location: http://mydomain.com/images.php
    Line Number 7, Column 1:

  3. Eric Nagel
    3095 days ago

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

  4. Panda
    3094 days ago

    Well, it’s the tea site, wegottea.com, 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”);

  5. Panda
    3094 days ago

    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?

  6. Linda
    2473 days ago

    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?


    • Eric Nagel
      2468 days ago

      Look at the Datafeed series from step 1.

      vars.php is a file that contains your database connection & other common variables.

Leave a comment

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