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

谢谢。

最佳答案

对于此问题的客户端,这是我用来打包作物箱数据并将其运送到服务器的代码。参见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),我处理并存储以下坐标:

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

关于javascript - cropper.js上传带有坐标的原始图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41943931/

10-09 23:19