[Zope-dev] Generated Images Again

Jeff Rush Jeff Rush" <jrush@timecastle.net
Fri, 12 Nov 99 19:11:43 -0500


On Fri, 12 Nov 1999 11:48:33 -0800, David S. Harrison wrote:

>Well, this successfully calls the function but substitutes
>the bits from the PNG image directly into the <IMG> tag.
>I mean the image tag becomes: '<IMG SRC="jfdla;jgadjklh32kjhkjhfdkahlfdsa;'g...">'
>where the garbage is the bits from the image.  The browser barfs
>on this in a major way.  Looks like it is still not quite there.

You need to look at it as two transactions -- one to generate the
HTML and another when the browser _comes back_ and gets
the image.  Now since you can't easily cache the image btw
those two times, you need to defer generation of the image
until the browser asks for it.

So in your DTML code, you need to return a URL indicating
_where_ to get the image and _what parameters_ to use
when doing so.

   <IMG SRC="<dtml-var SCRIPT_NAME>/GenerateImage?xsize=300&colors=16">>

And then create an external method named "GenerateImage" that
looks like:

def GenerateImage(self, xsize=800, ysize=600, colors=256, REQUEST=None, RESPONSE=None):
    # Spin the Pixels -now- .....

    if REQUEST and 'image/png' in map(strip, split(REQUEST.get_header('Accept', ''), ',')):
        outformat = 'PNG'; outmime = 'image/png'
    else:
        outformat = 'GIF'; outmime = 'image/gif'

    if RESPONSE:
        RESPONSE.setHeader('content-type', outmime)

    ofd = StringIO() ; im.save(ofd, outformat)
    return ofd.getvalue()