This tutorial will demonstrate how you can prevent other people from displaying YOUR images on their websites.

By this I mean people actually loading images on their site, from your website. This is a big problem is it will increase the bandwidth used on your account and therefore increasing your expenses. Many of you have seen the same thing in you use AngelFire free hosting. If you have an account with them and you want to display your image:

 

Now, unless the image is being called from within the AngelFire system, you get an ugly image saying "We're sorry, this image is hosted by AngelFire". Creating a system that does this is fairly easy in ColdFusion. Let me show you how it's done.

Now before I get to the explanation, some of you might be saying, wait a minute.. what do you mean? I've never used AngelFire...

So, I'll explain by showing, in the past when you wanted to display your images you used the standard:

	My Logo

The problem with this is that people would view your source or right click on the image and see the direct path to your image. This could be a problem, specially if you don't want your image to be displayed on other places by having it be loaded from your server. Anyone out there could simply use:

 
	My Logo

This could cost you lots of money, specially if it's a very popular image :)

Your alternative? This tutorial...

You can now load your images as follows:

 
	My Logo

Pretty nifty, huh? ok, let me explain what you must do to achieve this... the first thing we need to do is to create a file called: load_image.cfm

 
	<!--- File load_image.cfm --->
	<!--- First define the PHYSICAL path to the images --->
	<cfset images_path = "C:\inetpub\wwwroot\mysite.com\images\" />

The image path above will be where all the images are stored, the beauty of this script is that the images can even be in a folder that is NOT internet accessible.

 
	<!--- The next thing we do is verify that the image is being loaded from our site, and not someone else's --->
	<cfif NOT CGI.REFERER contains "mysite.com">
		<!--- Someone is trying to load this image from another place, stop them! --->
		<!--- The first thing we MUST do is load the smaller/error message file --->
		<cfcontent type="image/gif" file="#images_path#noleech.gif" />
		
	<cfelse>
		<!--- image is good, server the content --->
		<cfif Right(Image, 3) eq "gif">
			<cfcontent type="image/gif" file="#images_path##Image#" />
		<cfelseif Right(Image, 3) eq "jpg">
			<cfcontent type="image/jpg" file="#images_path##Image#" />
		</cfif>
	</cfif>

OK, what the code above is doing is basically this:

  • First, check to make sure that the image being called is coming from YOUR site and not someone else's.
    • If the call IS coming from your site, then it loads the image required and servers it to the calling web page.
    • If the call IS NOT coming from your site, it will load a different (SMALLER) image, therefore saving you bandwidth and not allowing that individual to load the image from their site.
  • The next thing is does is basically output the image. So that the page calling it will display it correctly.

Now on the pages you want the images to display you basically do this:

 
	
My Image Caption

this will ONLY work if it is being loaded from your web site, so if people view the source on the page, they will see this and not the actual path to the image itself.

NOTE: If you don't have an image to display when people are trying to steal your content, try this one:


To save right-click on it and save to your computer.

 
	<!---
		Notice that this image is being served as follows:
http://66.92.207.195/load_image.cfm?Image=noleech.gif --->

Need Another possible use for this tutorial?

Maybe if some of you want to know how many times an image was loaded. You could use this script to implement a counter system for your images. Remember that you have a .CFM page loading the image, so you could do anything you wanted right before serving the image. :-)

Well, That's pretty much it... Remember, this script will NOT stop people from downloading the images to their hard drives (That's pretty much impossible) but by hiding the paths to your images, people cannot load your images on their sites, using your bandwidth.

Questions? Comments? Email Me...

About This Tutorial
Author: Pablo Varando
Skill Level: Advanced 
 
 
 
Platforms Tested: CF5,CFMX
Total Views: 65,417
Submission Date: August 28, 2010
Last Update Date: August 28, 2010
All Tutorials By This Autor: 47
Discuss This Tutorial
  • This may be a stupid question, but I have one anyway. I work for a City and we have a content management system. The system is built in coldfusion. I'm new to CF and have tried to use this code in my template forms and it errors out. Where is this code suppose to be placed?

  • yes, it can take longer to load, this is no longer a direct HTTP{ request to the image URL by the browser, cfcontent must read the file contents from the hard drive and spit it out to the browser.

  • Nice and simple to implement and works like a treat. The only downside is that images now take longer to appear on the page. Not sure how to combat this yet, I might play around with holding/stalling the page from the end user until it’s loaded completely.

  • Simple yet so useful. Awesome tutorial. As far as complaints from others - if you're so desperate to steal my photo (anti-right click) by print screen then crop, by all means knock yourself out :)

  • This is to SAVE BANDWIDTH from LEECHING. not to prevent downloads. sheesh. If you do this right, you CAN NOT guess the image folder name and browse to it, because it is BELOW the web root, served be CFCONTENT.

  • Solved my problem.

  • Yea, at first I was thinking....you can still take logo.gif...but then I saw the physical path and that really surprised me, I would never think of that. Great trick, awesome tutorial!

  • .. not sure, but would this stop me from just cleaning your url? I guess since it doesn't say WHERE .. it would make it hard: C:\inetpub\wwwroot\mysite.com\images I would have to GUESS its in the \images folder then. I see now! Good stuff. For those other guys .. yeah can they read ? This is a really nice trick.

  • Peter, If you read the tutorial, you would know what the intention was. Open mouth, insert foot.

  • Way to be a complete idiot Peter, maybe if you had a god damn clue about what this is for it woudln't be useless.

Advertisement

Sponsored By...
Powered By...