It's not very hard, here's a possibility that requires you to have pillow
<https://pillow.readthedocs.org/> installed.
auth.settings.extra_fields['auth_user'] = [
Field('photo', 'upload', label=T('Photo'), uploadseparate=True, requires
=[IS_EMPTY_OR(IS_IMAGE(error_message=T('This is not an image.'))), IS_LENGTH
(maxsize=1048576)]),
Field('avatar','upload', uploadseparate=True, readable=False, writable=
False, autodelete=True, compute=lambda row: SMARTHUMB('auth_user', row.photo
, (32, 32), name='avatar')),
]
def upload_fullpath(tablename, filename, makedirs=False):
"""
Given a tablename and a filename return the full path
TODO: For now it assumes uploadseparate=True in the field definition,
could be more versatile.
"""
import os
from gluon import current
fieldname, img_uid = filename.split('.')[1:3]
path = os.path.join(current.request.folder, 'uploads', tablename + '.' +
fieldname, img_uid[:2], filename)
if makedirs:
try:
os.makedirs(os.path.dirname(path))
except OSError:
print "Skipping creation of %s because it exists already." % os.
path.dirname(path)
return path
def SMARTHUMB(tablename, image, box, fit=True, name="thumbnail"):
"""
Downsample the image.
http://www.web2pyslices.com/slice/show/1522/generate-a-thumbnail-that-fits-in-a-box
Modified to respect uploadseparate=True differences
@param img: Image - an Image-object
@param box: tuple(x, y) - the bounding box of the result image
@param fit: boolean - crop the image to fill the box
"""
if image:
from PIL import Image
import os
from gluon.tools import current, web2py_uuid
request = current.request
img = Image.open(upload_fullpath(tablename, image))
#preresize image with factor 2, 4, 8 and fast algorithm
factor = 1
while img.size[0] / factor > 2 * box[0] and img.size[1] * 2 /
factor > 2 * box[1]:
factor *= 2
if factor > 1:
img.thumbnail((img.size[0] / factor, img.size[1] / factor),
Image.ANTIALIAS)
#calculate the cropping box and get the cropped part
if fit:
x1 = y1 = 0
x2, y2 = img.size
wRatio = 1.0 * x2 / box[0]
hRatio = 1.0 * y2 / box[1]
if hRatio > wRatio:
y1 = int(y2 / 2 - box[1] * wRatio / 2)
y2 = int(y2 / 2 + box[1] * wRatio / 2)
else:
x1 = int(x2 / 2 - box[0] * hRatio / 2)
x2 = int(x2 / 2 + box[0] * hRatio / 2)
img = img.crop((x1, y1, x2, y2))
#Resize the image with best quality algorithm ANTI-ALIAS
img = img.resize(box, Image.ANTIALIAS)
root, ext = os.path.splitext(image)
thumbname = '%s.%s.%s.%s%s' % (tablename, name, web2py_uuid().
replace('-', '')[-16:], web2py_uuid().replace('-', '')[-16:], ext)
img.save(upload_fullpath(tablename, thumbname, makedirs=True))
return thumbname
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.