[Zope] Newbie Question: Returning random images for use in <img src=''> links on other server
Oliver Bleutgen
myzope@gmx.net
Tue, 20 Nov 2001 18:39:32 +0100
Ben Chapman wrote:
> Everyone:
>
> Please excuse this if it is a FAQ; I've looked and I've got the book,
> but I'm am just not getting this.
>
> Our main web server (I can't change this) is an IIS box. I want to
> generate links to random images stored within zope.
>
> What I want to do (on the IIS box) is:
>
> <img src=http://myzopeserver/content/showimg>
>
> where showimg does something like this on myszopeserver:
>
> [BEGIN BROKEN CODE]
> <dtml-call "RESPONSE.setHeader('Content-Type', 'image/jpeg')">
> <dtml-var expr="_.whrandom.choice(files.objectValues())>"
> [END BROKEN CODE]
>
> In other words, I want the "raw" image data stored in one of the
> random files located in the files folder to be returned. This does
> not work; it seems I need the read() method of the file object, but I
> don't know how to integrate that with the construct above.
>
> Any help would be most appreciated. Sorry for the very basic question.
Hi Ben,
I don't think it's so basic. One method might be:
<dtml-call "REQUEST.set('rand_image_id',_.whrandom.choice(objectIds('Image')))">
<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">
To enable caching, you'll have to do some more work. You can look
at OFS/Image.py how it is done.
The problem with your method is a) it's "data", not "read()", and b) perhaps, that
you don't take into account that dtml-methods also return whitspaces you wrote
(before dtml-var are 2 newlines).
Oh, and be aware, I tested this only on internet explorer and via telnet,
and internet explorer is capable of showing an image when you just use
the last line (it analyzes the data stream and guesses the content-type).
You absolutly should check on netscape etc...
cheers,
oliver