[Zope] Newbie Question: Returning random images for use in <img src=''> links on other server

Ben Chapman benjamin-chapman@utulsa.edu
Tue, 20 Nov 2001 17:26:54 -0600


Thank you to Oliver Bleutgen for giving me the solution to this problem. 
  I wanted to have a link to a zope server from a non-zope server that 
would return a random zope stored image.

Here's the code that I finally used:

<dtml-call expr="REQUEST.set('rand_image_id', 
_.whrandom.choice(files.objectIds(['Image','File'])))">
<dtml-call 
"RESPONSE.setHeader('Content-Type',_.getitem(rand_image_id,0).content_type)">
<dtml-call 
"RESPONSE.setHeader('Content-Length',_.getitem(rand_image_id,0).size)">
<dtml-return "_.getitem(rand_image_id,0).data">
<dtml-call "RESPONSE.setHeader('Expires','Thu, 01 Aug 2000 09:00:00 GMT')">

Put this snippet in a DTML Document named showimages in the 'files' 
directory of your zope, along with the images and files that you wish to 
display. Then link to the code as <img 
src="http://myzopeserver/content/files/showimage"> and you'll get random 
images on your non-zope site. Caching issues are not yet addressed, 
except by returning the Expires header to the browser.

If the name of the directory is not 'files', change the name after the 
'_.whrandom.choice(' to point to the correct name. If I understood 
better what I was doing, I would have it point to something that stands 
for 'current directory'; however, this is still beyond me, and (at least 
with Zope 2.1.6, if you do 
_.whrandom.choice(objectIds(['Image','File']))) and omit the name of the 
  folder, the call fails. 'Self' does not work either.

Thanks! As usual, improvements to this solution are welcome!

Ben Chapman
benjamin-chapman@utulsa.edu