Advanced Image fallback and PiL

by Tom "Spanky" Kapanka last modified Dec 30, 2008 03:03 PM
A recipe of how to write a complex set of fall back logic for a scaled image

PROBLEM:

My custom folderish type has an image field called 'logo'.  An admin can also put an Image in the folder, named "logo.jpeg", which will be shown if it exists. (This is because it is easier to WebDAV up lots of logos to lots of folders, and I don't know how to handle binary data with ATCVSImport, which is how I create 100's of these types).  If the owner puts an image into the Logo Field (an ImageFIeld) it should show that instead.  If there is no image in the Image Field, and no file named logo.jpeg in the folder, show the default logo placeholder image instead.  Oh, did I mention everything is scaled using PiL?

ASSUMPTIONS:

You know how to use Archetypes
You have PiL installed and working
You have some sizes defined in your type's Image Field

SOLUTION:
<div id="logo">
	<img src="" tal:condition="here/logo|nothing"
		    tal:define="url python:here.absolute_url();"
		    tal:attributes="src string:${url}/logo_mini" />
		    
	<img src="logo_placeholder.jpeg" 
		    tal:condition="not:here/logo|nothing"
	            tal:replace="structure nocall:here/logo.jpeg|default" />
</div>

Better solution?  Let me know, I'll put it up!  Hope this helps.

~Spanky