Model:
db.define_table('uploads',
Field('name','string'),
Field('mainfile','upload'),
Field('thumb','upload',writable=False,readable=False),
)
Controller:
def makeThumbnail(dbtable,ImageID,size=(150,150)):
try:
thisImage=db(dbtable.id==ImageID).select()[0]
import os, uuid
from PIL import Image
except: return
im=Image.open(request.folder + 'uploads/' + thisImage.mainfile)
im.thumbnail(size,Image.ANTIALIAS)
thumbName='uploads.thumb.%s.jpg' % (uuid.uuid4())
im.save(request.folder + 'uploads/' + thumbName,'jpeg')
thisImage.update_record(thumb=thumbName)
return
def uploadimage():
dbtable = db.uploads #uploads table name
if len(request.args):
records = db(dbtable.id==request.args[0]).select()
if len(request.args) and len(records):
form = SQLFORM(dbtable, records[0], deletable=True)
else:
form = SQLFORM(dbtable)
if form.accepts(request.vars, session):
response.flash = 'form accepted'
makeThumbnail(dbtable,form.vars.id,(175,175))
elif form.errors:
response.flash = 'form has errors'
## Quick list just to demonstrate...
list = crud.select(dbtable)
return dict(form=form,list=list)
(I am grateful to Lukasz (aka ls1) for his kind assistance).
Comments (11)
- Login to post
order by: newest oldest upvoted downvoted
I did what I explained and it worked perfect. But when uploading in Amazon EC2 does not work.
There are dependencies to use this module?
- villas 11 years ago
You can do also:
Why can't I edit my own comment? Can somebody delete my first comment? Thanks
I deleted it and will add comment editing. I thought previewing would be enough.
show more comments0
gheither 11 years ago
This slice used to work for me about a year ago, now it's not after I upgraded my web2py. Can anyone get this working with the latest web2py build?
It creates the thumbnail successfully and stores it in the uploads folder, but when you try to retrieve the thumbnail through the URL bar or the DB admin screen you get a ticket.
<type 'exceptions.TypeError'> Can't retrieve uploads.thumb.16e0d9fd-bc66-4b36-9c75-03aaea1d8603.PNG
Please help!
0
eanappi 11 years ago
replies (1)
0
write2ariya 12 years ago
Dear David
This is very useful. I use you code in my project. Thank you for your contribution & sharing.
(just wanna thanks, I will payback web2py community someway)
Ariya
0
simpel 14 years ago
0
mrfreeze 14 years ago