Eric Nagel

Eric Nagel

CTO, PHP Programmer, Affiliate Marketer & IT Consultant

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:


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

  • Trackback: 9021-SEO & the Unknown Pixel
  • Panda
    Posted June 29, 2010 4:09 am 0Likes

    I got an error when running images.php

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

  • Eric Nagel
    Posted June 29, 2010 9:30 am 0Likes

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

  • Panda
    Posted June 30, 2010 1:19 am 0Likes

    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”);

  • Panda
    Posted June 30, 2010 1:25 am 0Likes

    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?

  • Linda
    Posted March 12, 2012 7:15 am 0Likes

    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
      Posted March 17, 2012 10:18 am 0Likes

      Look at the Datafeed series from step 1.

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

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.