def avatar_crop(request, id=None): """ Avatar management, creates a new avatar and makes it default """ if id: avatar = get_object_or_404(Avatar, id=id, user=request.user) else: avatar = get_object_or_404(Avatar, user=request.user, primary=True) if (avatar.avatar.width<=avatar.avatar.height): result = "width" else: result = "height" if not request.method == "POST": form = AvatarCropForm() else: try: orig = avatar.avatar.storage.open(avatar.avatar.name, 'rb').read() image = Image.open(StringIO(orig)) except IOError: return form = AvatarCropForm(image, request.POST) if form.is_valid(): top = int(form.cleaned_data.get('top')) left = int(form.cleaned_data.get('left')) right = int(form.cleaned_data.get('right')) bottom = int(form.cleaned_data.get('bottom')) box = [ left, top, right, bottom ] (w, h) = image.size if result=="width": box = map(lambda x: x*h/AVATAR_CROP_MAX_SIZE, box) else: box = map(lambda x: x*w/AVATAR_CROP_MAX_SIZE, box) image = image.crop(box) if image.mode != 'RGB': image = image.convert('RGB') thumb = StringIO() image.save(thumb, AVATAR_THUMB_FORMAT, quality=AVATAR_THUMB_QUALITY) thumb_file = ContentFile(thumb.getvalue()) base_name, ext = os.path.splitext(avatar.avatar.name) thumb = avatar.avatar.storage.save("%s_cropped%s" % (base_name, ext), thumb_file) Avatar.objects.filter(id=avatar.id).update(primary=False) new_avatar = Avatar(user=request.user, primary=True, avatar=thumb) new_avatar.save() request.user.message_set.create( message=ugettext("Your new avatar has been saved successfully.")) return HttpResponseRedirect(reverse("avatar_change")) return render_to_response("avatar_crop/crop.html", { 'AVATAR_CROP_MAX_SIZE': AVATAR_CROP_MAX_SIZE, 'dim': result, 'avatar': avatar, 'form': form }, context_instance=RequestContext(request))
def avatar_crop(request, id=None): """ Avatar management, creates a new avatar and makes it default """ if id: avatar = get_object_or_404(Avatar, id=id, user=request.user) else: avatar = get_object_or_404(Avatar, identity=request.user) if (avatar.original_image.width <= avatar.original_image.height): result = "width" else: result = "height" if not request.method == "POST": form = AvatarCropForm() else: try: orig = avatar.original_image.storage.open( avatar.original_image.name, 'rb').read() image = Image.open(StringIO(orig)) except IOError: return form = AvatarCropForm(image, request.POST) if form.is_valid(): avatar.cropped_image.delete() left = int(form.cleaned_data.get('left')) top = int(form.cleaned_data.get('top')) right = int(form.cleaned_data.get('right')) bottom = int(form.cleaned_data.get('bottom')) avatar.box = '%s,%s,%s,%s' % (left, top, right, bottom) avatar.cropped_image.generate(lazy=False) avatar.save() return render_to_response("avatar_crop/crop.html", { 'AVATAR_CROP_MAX_SIZE': AVATAR_CROP_MAX_SIZE, 'dim': result, 'avatar': avatar, 'form': form }, context_instance=RequestContext(request))
def avatar_crop(request, id=None): """ Avatar management, creates a new avatar and makes it default """ if id: avatar = get_object_or_404(Avatar, id=id, user=request.user) else: avatar = get_object_or_404(Avatar, identity=request.user) if (avatar.original_image.width<=avatar.original_image.height): result = "width" else: result = "height" if not request.method == "POST": form = AvatarCropForm() else: try: orig = avatar.original_image.storage.open(avatar.original_image.name, 'rb').read() image = Image.open(StringIO(orig)) except IOError: return form = AvatarCropForm(image, request.POST) if form.is_valid(): avatar.cropped_image.delete() left = int(form.cleaned_data.get('left')) top = int(form.cleaned_data.get('top')) right = int(form.cleaned_data.get('right')) bottom = int(form.cleaned_data.get('bottom')) avatar.box = '%s,%s,%s,%s' % (left, top, right, bottom) avatar.cropped_image.generate(lazy=False) avatar.save() return render_to_response("avatar_crop/crop.html", { 'AVATAR_CROP_MAX_SIZE': AVATAR_CROP_MAX_SIZE, 'dim': result, 'avatar': avatar, 'form': form }, context_instance=RequestContext(request))