[Zope-DB] Displaying an image from a MySQL blob
Aaron Fosdick
aaron at hypervision.com
Wed Jul 7 18:55:41 EDT 2004
Thanks for the response Bobb,
I solved my problem, and it was so simple! I'm surprised that nobody has
mentioned this yet.
The displayImage python script looks like:
photo = context.searchImage(pic_ID = pic_ID)
container.REQUEST.RESPONSE.setHeader('Content-type', 'image/' +
photo[0].filetype)
return context.rawimage(photo)
Next, I wrote an external method called rawimage - it looks like:
##bind container=container
##bind context=context
##bind namespace=
def makeImage(self, photo):
return photo[0].picture.tostring()
And it worked!
The makeImage function is arbitrary, but the tostring() command was all
it took to collapse the array and return a bytestream rather than the
array result. Also, this code only returns one image, so you would want
to use something like your method to return a number of images from a
single query.
The url works as an img src, ex:
<img src ="./displayImage?pic_ID=5">
Regards,
--Aaron
Bobb wrote:
>----- Original Message -----
>From: "Aaron Fosdick" <aaron at hypervision.com>
>To: "zope-db mailling list" <zope-db at zope.org>
>Sent: Thursday, July 01, 2004 4:57 PM
>Subject: [Zope-DB] Displaying an image from a MySQL blob
>
>
>
>
>>Hi - I've been battling this for a couple of days, and am hoping someone
>>here can help. I want to zope to display an image out of a MySQL blob
>>field. While uploading images works, displaying them has me stumped. I
>>see that a number of other people are running into this issue also.
>>
>>SQL method: searchImage:
>> select filetype, picture from image where
>> <dtml-sqltest pic_ID op="eq" type="int">
>>
>>Python Script: displayImage:
>> photo = context.searchImage(pic_ID = pic_ID)
>> container.REQUEST.RESPONSE.setHeader('Content-type', 'image/' +
>>photo[0].filetype)
>> return photo[0].picture
>>
>>
>>
>Here's my python script... hacked from someone else, probably.
>
>called with <img src="selectimage1?id=&dtml-temp;"alt="foto">
>maybe that will help
>bobb
>
># Manually iterate over whole result set, copy into list of dicts just like
>dictionaries()
># except we also read all BLOBs in their entirety and put in the value, not
>the
># LobLocator
>
>result = []
>lob = ['image1']
>for row in context.sql_table_image1(id=id):
> lob = row['image1']
># result.append({'image1': lob.read()})
># above was remmed
>container.REQUEST.RESPONSE.setHeader('content-type', 'image/jpeg')
>return lob
>
>
>
>
>
>>Ideally, something like: <img src ="./displayImage?pic_ID=5"> would
>>serve up a jpeg.
>>
>>I've followed Murphy's how-to for "Upload, Insert and Retrieval of
>>BLOBs", but that doesn't work since the Content-Type doesn't match the
>>page content - which is text containing escaped data. This is the same
>>result as 'test' in the SQL method:
>> array('c', '\xff\xd8\xff......')
>>This wouldn't work anyway as I understand that zope security won't allow
>>you to serve non-html from a python script.
>>
>>
>>So there are three options that I've seen and that's where I'm getting
>>stuck. From an efficiency perspective, which of the below is the best
>>method and what would the code look like?
>> 1. External method - in the python script above, use: return
>>context.extMethod(photo[0].picture)
>> This calls the method and converts the array into data
>> 2. External method as a brain - pass the query result and process it
>>there.
>> 3. Don't use the displayImage python script at all. Do everything in
>>the external method, including the sql query.
>> Is there any way to just get the raw data into a string in
>>non-escaped form? I imagine that would be the least costly.
>>
>>I've been working on the first method, but can't seem to get a result as
>>anything other than text or elements of the array.
>>
>>Thanks for any assistance
>>
>>--Aaron
>>
>>
>>_______________________________________________
>>Zope-DB mailing list
>>Zope-DB at zope.org
>>http://mail.zope.org/mailman/listinfo/zope-db
>>
>>
>>
More information about the Zope-DB
mailing list