예제 #1
0
파일: models.py 프로젝트: chendong0444/ams
def save_avatar_in_db(sender, uid, avatar_name, **kwargs):
    if Profile.objects.filter(user_id=uid).exists():
        profile = Profile.objects.get(user_id=uid)
        old_avatar_name = profile.avatar_name
        Profile.objects.filter(user_id=uid).update(avatar_name=avatar_name)

        # delete old avatar on qiniu cloud
        storage = get_default_storage()
        fpath = '%s%s.%s' % (UPLOAD_AVATAR_AVATAR_ROOT, old_avatar_name, UPLOAD_AVATAR_SAVE_FORMAT)
        if storage.exists(fpath):
            storage.delete(fpath)
    else:
        Profile.objects.create(user_id=uid, avatar_name=avatar_name)
예제 #2
0
파일: utils.py 프로젝트: siolag161/tendenci
def resize_s3_image(image_path, width=200, height=200):
    """
    Resize an image on s3.
    The image_path is the relative path to the media storage.
    """
    storage = get_default_storage()
    f = storage.open(image_path)
    content = f.read()
    f.close()
    img = Image.open(StringIO(content))
    img.thumbnail((width, height), Image.ANTIALIAS)
    f = storage.open(image_path, 'w')
    img.save(f)
    f.close()
예제 #3
0
파일: utils.py 프로젝트: BIGGANI/tendenci
def resize_s3_image(image_path, width=200, height=200):
    """
    Resize an image on s3.
    The image_path is the relative path to the media storage.
    """
    storage = get_default_storage()
    f = storage.open(image_path)
    content = f.read()
    f.close()
    img = Image.open(StringIO(content))
    img.thumbnail((width,height),Image.ANTIALIAS)
    f = storage.open(image_path, 'w')
    img.save(f)
    f.close()
예제 #4
0
def crop_avatar(request):
    try:
        upim = UploadedImage.objects.get(uid=get_uid(request))
    except UploadedImage.DoesNotExist:
        raise UploadAvatarError(UPLOAD_AVATAR_TEXT['NO_IMAGE'])

    image_orig = upim.get_image_path()
    if not image_orig:
        raise UploadAvatarError(UPLOAD_AVATAR_TEXT['NO_IMAGE'])

    try:
        x1 = int(float(request.POST['x1']))
        y1 = int(float(request.POST['y1']))
        x2 = int(float(request.POST['x2']))
        y2 = int(float(request.POST['y2']))
    except:
        raise UploadAvatarError(UPLOAD_AVATAR_TEXT['ERROR'])

    try:
        orig = Image.open(image_orig)
    except IOError:
        raise UploadAvatarError(UPLOAD_AVATAR_TEXT['NO_IMAGE'])

    orig_w, orig_h = orig.size
    if orig_w <= border_size and orig_h <= border_size:
        ratio = 1
    else:
        if orig_w > orig_h:
            ratio = float(orig_w) / border_size
        else:
            ratio = float(orig_h) / border_size

    box = [int(x * ratio) for x in [x1, y1, x2, y2]]
    size = UPLOAD_AVATAR_RESIZE_SIZE[-1]
    avatar = orig.crop(box)
    avatar = avatar.resize((size, size), Image.ANTIALIAS)
    avatar_name, _ = os.path.splitext(upim.image)
    tmp_io = StringIO.StringIO()
    avatar.save(tmp_io,
                UPLOAD_AVATAR_SAVE_FORMAT,
                quality=UPLOAD_AVATAR_SAVE_QUALITY)
    tmp_file = InMemoryUploadedFile(tmp_io, None, upim.image, 'image/jpeg',
                                    tmp_io.len, None)
    fpath = '%s%s.%s' % (UPLOAD_AVATAR_AVATAR_ROOT, avatar_name,
                         UPLOAD_AVATAR_SAVE_FORMAT)
    storage = get_default_storage()
    if not storage.exists(fpath):
        storage.save(fpath, tmp_file)

    # def _resize(size):
    #     res = avatar.resize((size, size), Image.ANTIALIAS)
    #     res_name = '%s-%d.%s' % (avatar_name, size, UPLOAD_AVATAR_SAVE_FORMAT)
    #     res_path = os.path.join(UPLOAD_AVATAR_AVATAR_ROOT, res_name)
    #     res.save(res_path, UPLOAD_AVATAR_SAVE_FORMAT, quality=UPLOAD_AVATAR_SAVE_QUALITY)
    #
    # for size in UPLOAD_AVATAR_RESIZE_SIZE:
    #     _resize(size)

    avatar_crop_done.send(sender=None,
                          uid=get_uid(request),
                          avatar_name=avatar_name)
    if UPLOAD_AVATAR_DELETE_ORIGINAL_AFTER_CROP:
        upim.delete()

    # return redirect(reverse('profile.index'))
    return HttpResponse(
        "<script>window.parent.crop_avatar_success('%s')</script>" %
        UPLOAD_AVATAR_TEXT['SUCCESS'])