Create Dynamic Banner Ads for Blogs & News Sites

Published: February 12th, 2009 by:

When you visit a site, their usual advertisers might catch your eye once or twice, but after that, you forget it's there. Designing a banner ad that automatically updates with information such as current sales or latest blog posts is a great way to get repeat clicks and return visitors. In this article, I'm going to explain the code behind a dynamic ad implementation I use right here on this site.


If you go look at our Promote this Site page, you will see that a dynamic ad is offered that shows the most recent article on this site. This is accomplished by creating a PHP script that reads this site’s RSS Feed, finding the latest post title, and overlaying it onto a graphic template.

I think this concept has potential with blogs and news sites because static banner ads become holes in the page to repeat visitors. If I get somebody interested in this site once, then that banner will continue to catch their eyes when the article changes, and if they see a title that interests them again, they will visit the site again to read the latest article.

Here is some sample code based on what I have implemented on this site’s advertising page:

Run This Example

<?php
	/* there is no need to call the feed up every time, so we will cache it for 1 hour */
	if (!file_exists('feed.cache') || filemtime('feed.cache') + 3600 < time())
	{
		/**
		 * Looks like we don't have a recent copy, so we will use Snoopy to fetch the remote feed.
		 * See: http://phpstarter.net/2008/12/how-to-post-data-and-fetch-remote-pages-from-php-scripts/
		 */
		require('../includes/Snoopy.class.php');
		$snoopy = new Snoopy();
		
		/* set this to the RSS feed where we want to fetch the title from */
		$snoopy->fetch('http://phpstarter.net/feed/');
		
		/* save this to a local cache file so the RSS feed isn't called too often */
		file_put_contents('feed.cache', $snoopy->results);
	}
	
	/**
	 * We will use lastRSS to parse the feed.
	 * See: http://phpstarter.net/2009/01/load-rss-feeds-into-php-arrays-with-lastrss/
	 */
	require('../includes/lastRSS.php');
	$rss = new lastRss();
	
	/* load the cached file */
	$feed = $rss->get('feed.cache');
	
	/* this is the banner add that we will overlay the text on top of */
	$im = imagecreatefromgif('banner_ad_article.gif');
	
	/* set some colors */
	$black = imagecolorallocate($im, 0, 0, 0);
	$white = imagecolorallocate($im, 255, 255, 255);
	$orange = imagecolorallocate($im, 249, 160, 33);
	
	/* load the font we want to use */
	$f_domestic = '../includes/Domestic_Manners.ttf';
	
	/* this is the title taken from the RSS feed's first (or most recent) item */
	$title = $feed['items'][0]['title'];
	
	/* get the width of the to-be-printed text so we can center it */
	$bbox = imagettfbbox(12, 0, $f_domestic, $title);
	$x = $bbox[0] + (imagesx($im) / 2) - ($bbox[4] / 2);
	
	/* write the text to the base image */
	imagettftext($im, 12, 0, $x, 52, $orange, $f_domestic, $title);
	
	/* tell the browser that we are outputting a GIF image */
	header('Content-type: image/gif');
	
	/* render the image */
	imagegif($im);
?>

Leave a Reply





Wordpress doesn't like it when you post PHP code. Go save your code at pastebin, and post the link here.

About the Author

Andrew has been coding PHP applications since 2006, and has plenty of experience with PHP, MySQL, and Apache. He prefers Ubuntu Linux on his desktop and has plenty of experience at managing CentOS web servers. He is the owner of Wells IT Solutions LLC, and develops PHP applications full time for anyone that needs it as well as does desktop computer support locally in the local area. He spends most of his free time exploring new programming concepts and posting on The Webmaster Forums.