【发布时间】:2018-12-03 11:57:56
【问题描述】:
我有两个不同的部分用于在网站上上传图片,一个用于横幅,另一个用于个人资料图片。它们也有不同的维度。上传图像在覆盖页面中工作。
我在我的服务器中添加了图像裁剪,根据我的代码,当用户单击横幅图像时,它会自动将其尺寸设置为图像裁剪,当用户单击个人资料图片时,它将个人资料图片大小设置为图像裁剪。
我想要的是,当用户上传照片然后取消它时,一切都会消失。现在如果用户在横幅照片中上传照片,然后取消它,当我们回到上传图片的叠加层时,照片仍然可用。此外,如果用户单击个人资料图片,它会再次显示横幅的照片及其尺寸。
我希望当用户单击取消按钮(在我的代码中为 .fa-times)时,有关上传图像及其尺寸的所有内容都将被删除。然后,如果用户单击另一个部分(个人资料照片或横幅),用户会看到一个具有正确尺寸的新页面。
这是我的代码,这是代码,当用户单击浏览按钮选择要上传的图像时。
this.body.addClass("overlay--active");
var imageBody = $(e.target).closest(".icon");
this.height = imageBody.attr("data-height");
this.width = imageBody.attr("data-width");
var crop = new Croppie(document.getElementById('js-image-editor'), {
enableExif: true,
showZoomer: true,
viewport: { width: this.width, height: this.height, type: 'square'},
boundary: { width: this.width, height: this.height}
});
function readFile(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('.section').addClass('ready');
// crop.croppie('bind', {
crop.bind({
url: e.target.result
}).then(function(){
console.log('jQuery bind complete');
});
}
reader.readAsDataURL(input.files[0]);
}
else {
swal("Sorry - you're browser doesn't support the FileReader API");
}
}
$('.button-upload-image').on('change', function () { readFile(this); });
$('.fa-save').on('click', function (ev) {
crop.result ({
type: 'canvas',
size: 'viewport'
}).then(function () {
console.log('upload image complete');
});
});
$(".fa-times").click(function() {
$('.section').removeClass('ready');
crop.bind ({
url: ''
}).then(function(){
console.log('reset complete');
});
})
正如您在最后一个块中看到的,我尝试重置图像裁剪,但它不起作用。每次出现“Croppie:不能多次初始化croppie”的错误。
有人可以帮我解决这个问题吗?我非常感谢您提前提供的帮助。
【问题讨论】:
标签: javascript jquery image crop