[Zope-CMF] CMF-HOWTO: automatically make thumbnails on image upload
Brad Clements
bkc@murkworks.com
Wed, 18 Jul 2001 18:58:08 -0400
A mini-howto
I have a custom portal type derived from a Zclass and Portal Folder. Users will be able
to upload multiple images as child objects of this type.
I want to automatically make a thumbnail when the image is uploaded.
To do this, I customized image_edit (here it is)
import PIL.Image
import StringIO
file=REQUEST.file # work around hang "bug" in CMF-1.1 because file doesn't get passed if custom image_edit used
context.edit(
precondition=precondition,
file=file)
qst='?portal_status_message=Image+changed.'
parentFolder=context.REQUEST.PARENTS[1]
if parentFolder.meta_type == 'CompanyProduct' and \
context.getId()[-10:] != '_Thumbnail':
thumbName = context.getId()+'_Thumbnail'
newthumb=getattr(parentFolder,thumbName,None)
if not newthumb:
newthumb = parentFolder.invokeFactory('Image',thumbName,RESPONSE=None)
file.seek(0)
im = PIL.Image.open(file)
im.thumbnail((200,200))
outFile = StringIO.StringIO()
im.save(outFile,'jpeg')
outFile.seek(0)
newthumb.manage_upload(outFile)
context.REQUEST.RESPONSE.redirect( context.absolute_url() + '/image_edit_form' + qst )
--
Then I had to create a PILProduct that has this __init__.py ( to allow Python scripts to
get access to PIL)
from Products.PythonScripts.Utility import allow_module, allow_class
from StringIO import StringIO
import PIL.Image
allow_class(PIL.Image.Image)
allow_module('PIL.Image')
allow_class(StringIO)
allow_module('StringIO')
--
Unfortunately I had to use StringIO instead of cStringIO, because you can't pass
cStringIO.StringIO to allow_class()
Brad Clements, bkc@murkworks.com (315)268-1000
http://www.murkworks.com (315)268-9812 Fax
netmeeting: ils://ils.murkworks.com AOL-IM: BKClements