Turn hotlinks into advertising - part II

In Monday's entry, I explained some simple techniques to turn a drain on resources into a free 1 method of advertising.

In today's entry, I'll be going into more depth about how to use a little PHP to dynamically generate your adverts. Why would you want to do this? For starters, you can modify the image to include a discount code, which will help track where the orders came from. Who knows, you could even strike up a deal with the site owner to display ads on their site should it bring you a lot of customers.

Please note that most of the code will just be fragments, and I'm not tying it down to one implementation. It's quite likely that I'll modify this method over time and post updates in the future. There's also the small question of “is it worth it”. Again, I'll keep you updated.

First things first

First, we'll create a database table to store several things:

  • The full referring URL – The unique identifier.
  • The number of hits to this file.
  • The discount code, either for this URL or for the domain.

Nothing particularly complex there, although you may wish to tweak it to allow for different advert styles.

Basic PHP

Our file, which I'll call "image-script.php" needs to carry out several functions:

  • Check to see if we've had a referrer from this site before.
    • If we have, log the hit and get the discount code
    • If we have not, create an entry and generate a discount code.
  • Create and output an image

Lets look at some sample code :

// Get the referrer and look for this site in the database
// Please note: HTTP_REFERER is not a typo
$referrer_url = $_SERVER['HTTP_REFERER'];
$siteData = find_site($referrer_url);

if ($siteData) {
    // Seen the site before, so grab discount code
    $discountCode = $siteData['discount_code'];
} else {
    // A new site, so create a discount code and add it to the db
    $discountCode = create_discount_code($refferer_url);
    add_new_site($referrer_url, $discountCode);

// Image Creation code

create_discount_code would need to be tailored to each particular solution, and I've deliberately left out the database code to save space.

Image Creation Code

The final bit of code is creating our image, complete with embedded discount code. You'll need the GD engine installed to use these functions. More information about that is available in the image section of the PHP manual.

The code itself is relatively simple. We open an advert image, and then add the discount code to it before outputting it to the browser.

// Image Creation code

// Setup where we want the code to appear
$codeX = 5;
$codeY = 5;

// Load advert image.
$advertImage = imagecreatefrompng('background-image.png');

// Draw discount code
$textColour = imagecolorallocate($advertImage, 255, 255, 255);
imagestring($advertImage, 3, $codeX, $codeY, $discountCode, $textColour);

// Output our image and cleanup
header('content-type: image/png');

In case you're wondering if creating an image works, here's one I made earlier (using slightly modified code for the shadow and position).


One of the advantages of using dynamically generated images is that you can modify the code to perform a split test if you wish to check ad wordings or appearances. That falls outside the scope of this article, but I may write about it in future.

All done

So there we have it – how to change an unpleasant situation into something that can benefit your business.

The code is still a little rough around the edges, so I'll be cleaning it up to make it ready for release. My goal is to create something that is simple to install and use, which will take some work.

In case anybody's wondering, I (was) using "iG::Syntax Hiliter" to produce the highlighted code snippets.



Not including bandwidth costs, but seeing as your bandwidth was already being stolen at least now you're getting some value out of it.

Post a comment

org-mode tags allowed: /italic/, *bold* and =code=