cropper.js使用坐标上传原始图像

时间:2017-01-30 19:28:00

标签: javascript file-upload image-uploading cropper

我正在使用cropper.js。我想上传原始图像和裁剪坐标(x,y,宽度,高度)而不是裁剪后的图像。这样做的首选方法是什么?

感谢。

1 个答案:

答案 0 :(得分:0)

对于这个问题的客户端,这里是我用来打包裁剪框数据并将其发送到服务器的代码。见esp:

formData.append('last_crop', JSON.stringify($imageBox.cropper('getCropBoxData')));

$('#crop_button').click(function(){
    // Upload cropped image to server if the browser supports `HTMLCanvasElement.toBlob`.
    // Store crop coordinates to db for future visit.
    var canvas = $imageBox.cropper('getCroppedCanvas');
    canvas.toBlob(function (blob) {
        var formData = new FormData();
        formData.append('croppedImage', blob);  // 'croppedImage' is the sent filename
        formData.append('last_crop', JSON.stringify($imageBox.cropper('getCropBoxData')));

        $.ajax('{% url 'profile_crop_avatar' %}', {
            method: "POST",
            data: formData,
            processData: false,
            contentType: false,
            success: function () {
                console.log('Upload success');
            },
            error: function () {
                console.log('Upload error');
            }
        });
    }, "image/jpeg", 0.75);

    // Also update masthead image after crop
    $('#masthead-avatar').attr('src', canvas.toDataURL());
});

在服务器端(Django),我处理和存储coords:

# Save new image and crop coords to profile
p = request.user
p.last_crop_coords = request.POST.get('last_crop')
p.save()