Пример #1
0
def avatarcrop(request):
    """
    Avatar management
    """
    avatar = get_object_or_404(Avatar, user=request.user, valid=False)
    if not request.method == "POST":
        form = AvatarCropForm()
    else:
        form = AvatarCropForm(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'))

            image = Image.open(avatar.image.path)
            box = [ left, top, right, bottom ]
            image = image.crop(box)
            if image.mode not in ('L', 'RGB'):
                image = image.convert('RGB')

            image.save(avatar.image.path)
            avatar.valid = True
            avatar.save()
            return HttpResponseRedirect(reverse("profile_avatar_crop_done"))

    template = "userprofile/avatar/crop.html"
    data = { 'section': 'avatar', 'avatar': avatar, 'form': form, }
    return render_to_response(template, data, context_instance=RequestContext(request))
Пример #2
0
def crop_avatar(request):
    form = AvatarCropForm(request.POST or None, request.FILES or None)
    profile = get_object_or_404(UserProfile, user=request.user)
    avatar = profile.avatar

    if avatar.width <= avatar.height:
        result = "width"
    else:
        result = "height"

    if settings.AVATAR_CROP_MAX_SIZE > max(avatar.width, avatar.height):
        AVATAR_CROP_MAX_SIZE = max(avatar.width, avatar.height)
    else:
        AVATAR_CROP_MAX_SIZE = settings.AVATAR_CROP_MAX_SIZE

    if request.method == "POST":
        try:
            orig = avatar.storage.open(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["top"])
            left = int(form.cleaned_data["left"])
            right = int(form.cleaned_data["right"])
            bottom = int(form.cleaned_data["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, settings.AVATAR_THUMB_FORMAT,
                    quality=settings.AVATAR_THUMB_QUALITY)
            thumb_file = ContentFile(thumb.getvalue())

            base_name, ext = os.path.splitext(avatar.name)
            profile.avatar = avatar.storage.save(
                    "%s_cropped%s" % (base_name, ext), thumb_file)
            profile.save()

            return redirect("view_myprofile")

    return render_to_response("account_crop_avatar.html",
            RequestContext(request, {
                    'AVATAR_CROP_MAX_SIZE': AVATAR_CROP_MAX_SIZE,
                    'dim': result,
                    'avatar': avatar,
                    'form': form
                    }))
Пример #3
0
def avatarcrop(request):
    """
    Avatar management
    """
    if (request.user.password == "!"):
        return HttpResponseRedirect('/accounts/social')
    avatar = get_object_or_404(Avatar, user=request.user, valid=False)
    if not request.method == "POST":
        form = AvatarCropForm()
    else:
        form = AvatarCropForm(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'))
            """
            image = Image.open(avatar.image.path)
            box = [ left, top, right, bottom ]
            image = image.crop(box)
            if image.mode not in ('L', 'RGB'):
                image = image.convert('RGB')

            image.save(avatar.image.path)
            avatar.valid = True
            avatar.save()
            """
            return HttpResponseRedirect(reverse("profile_avatar_crop_done"))

    template = "userprofile/avatar/crop.html"
    data = {
        'section': 'avatar',
        'avatar': avatar,
        'form': form,
    }
    return render_to_response(template,
                              data,
                              context_instance=RequestContext(request))
Пример #4
0
def crop_avatar(request):
    form = AvatarCropForm(request.POST or None, request.FILES or None)
    profile = get_object_or_404(UserProfile, user=request.user)
    avatar = profile.avatar

    if avatar.width <= avatar.height:
        result = "width"
    else:
        result = "height"

    if settings.AVATAR_CROP_MAX_SIZE > max(avatar.width, avatar.height):
        AVATAR_CROP_MAX_SIZE = max(avatar.width, avatar.height)
    else:
        AVATAR_CROP_MAX_SIZE = settings.AVATAR_CROP_MAX_SIZE

    if request.method == "POST":
        try:
            orig = avatar.storage.open(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["top"])
            left = int(form.cleaned_data["left"])
            right = int(form.cleaned_data["right"])
            bottom = int(form.cleaned_data["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,
                       settings.AVATAR_THUMB_FORMAT,
                       quality=settings.AVATAR_THUMB_QUALITY)
            thumb_file = ContentFile(thumb.getvalue())

            base_name, ext = os.path.splitext(avatar.name)
            profile.avatar = avatar.storage.save(
                "%s_cropped%s" % (base_name, ext), thumb_file)
            profile.save()

            return redirect("view_myprofile")

    return render_to_response(
        "account_crop_avatar.html",
        RequestContext(
            request, {
                'AVATAR_CROP_MAX_SIZE': AVATAR_CROP_MAX_SIZE,
                'dim': result,
                'avatar': avatar,
                'form': form
            }))