## put these in your settings.py ##
import os.path

#image on demand settings, writable path by webserver
IMAGE_ON_DEMAND_DIR = os.path.join(MEDIA_ROOT,'image_on_demand/')

#image widths allowed 
ALLOWED_WIDTHS=('600','639','700','800')

#usm settings for thumbnails, optional if you use my unsharp mask snippet
RADIUS=1
SIGMA=0.5
AMOUNT=0.8
THRESHOLD=0.016
#################

## urls.py ##

# this is what I have in my urls.py file
# object_id is contains the primary key of my photo database table, 'width' is the width of the 
# image wanted
# to the public the URL looks like pointing to a real picture :http://your.domain/photo/36/600.jpg
(r'^photo/(?P<object_id>\d+)/(?P<width>\d+).jpg$', 'gallery.views.image_on_demand'),



## inside views.py of my gallery app ##

def image_on_demand(request,object_id,width):
    
    if width in settings.ALLOWED_WIDTHS:
    	#get photo
	photo=Photo.objects.get(pk=object_id)
	
    	#path to original image and file split
    	original_file=os.path.join(settings.MEDIA_ROOT,photo.image_orig.name)
    	filehead, filetail = os.path.split(original_file)
    	
	#check if image path exists otherwise create it
	image_path=os.path.join(settings.IMAGE_ON_DEMAND_DIR,width)
	if not os.path.exists(image_path):
		os.mkdir(image_path)
	
	#create image path. note, width SHOULD be a string otherwise os.path.join fails
	image_file=os.path.join(settings.IMAGE_ON_DEMAND_DIR,width,filetail)
	
	# we need te calculate the new height based on the ratio of the original image, create integers
    	ratio=float(float(photo.image_orig.width) / float(photo.image_orig.height))
	height=int(float(width)/ratio)
	width=int(width)
	
	
	# check if file exists and the original file hasn't updated in between
	if os.path.exists(image_file) and os.path.getmtime(original_file)>os.path.getmtime(image_file):
    		os.unlink(image_file)
    
        # if the image wasn't already resized, resize it.Maybe I should rewrite it to do this directly with PythonMagick
	# taken from snippet http://www.djangosnippets.org/snippets/453/
	
        if not os.path.exists(image_file):
		image = Image.open(original_file)
		#assert False
    		image.thumbnail([width, height], Image.ANTIALIAS)
		format=image.format # preserve the format as it is lost in the USM step
	        
		#optional unsharp mask using snippet http://www.djangosnippets.org/snippets/1267/
		#image = usm(image,settings.RADIUS,settings.SIGMA,settings.AMOUNT,settings.THRESHOLD)
	
    		try:
        		image.save(image_file, format, quality=90, optimize=1)
    		except:
    			image.save(image_file, format, quality=90)
	
		
	image_data = Image.open(image_file)
	response = HttpResponse(mimetype="image/%s"%image_data.format) # create the proper HttpResponse object
	
	try:
        	image_data.save(response, image_data.format, quality=90, optimize=1)
    	except:
    		image_data.save(response, image_data.format, quality=90)
	
	return response