我想在用户上传时剪裁图片,剪裁后我想将其保存在数据库中。 我想用cropper或cropperjs来做。 如果我使用服务器中的图像一切都很好,但是当我想使用上传的文件时,剪切器就无法工作。
<input type="file" id="fileinput" accept="image/*" />
<button class="btn btn-default" data-toggle="modal" data-target=".bs-example-modal-lg">Large modal</button>
<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header" id="gallery">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myLargeModalLabel">Modal title</h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-sm btn-dark" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-sm btn-default">Save changes</button>
</div>
</div>
</div>
</div>
@section Scripts{
@*<link href="~/lib/cropperjs/dist/cropper.css" rel="stylesheet" />
<script src="~/lib/cropperjs/dist/cropper.js"></script>*@
<link href="~/lib/cropper/dist/cropper.css" rel="stylesheet" />
<script src="~/lib/cropper/dist/cropper.js"></script>
<script>
window.onload = function () {
var image;//= document.getElementById('image');
var cropper;
$("#btn").click(function (event) {
cropper.getCroppedCanvas().toBlob(function (blob) {
var formData = new FormData();
formData.append('croppedImage', blob);
$.ajax('/Home/Upload', {
method: "POST",
data: formData,
processData: false,
contentType: false,
success: function () {
console.log('Upload success');
},
error: function () {
console.log('upload Error');
}
})
});
});
}
var uploadfiles = document.querySelector('#fileinput');
uploadfiles.addEventListener('change', function () {
var files = this.files;
if (files.length > 0) {
previewImage(this.files[0]);
image = document.getElementById('image');
$('#image').cropper({
aspectRatio: 16 / 9,
viewMode: 1,
autoCropArea: 1,
minContainerWidth: 700,
minContainerHeight:600
});
}
}, false);
function previewImage(file) {
var galleryId = "gallery";
var gallery = document.getElementById(galleryId);
var imageType = /image.*/;
if (!file.type.match(imageType)) {
alert("File Type must be an image");
}
var thumb = document.createElement("div");
thumb.classList.add('thumbnail'); // Add the class thumbnail to the created div
var img = document.createElement("img");
img.id = "image";
img.style = "max-width:100%";
//img.src = "images/banner1.svg"; //if I use this line everything is fine!!!!
img.file = file;
thumb.appendChild(img);
gallery.innerHTML = '';
gallery.appendChild(thumb);
// Using FileReader to display the image content
var reader = new FileReader();
reader.onload = (function (aImg) { return function (e) { aImg.src = e.target.result; }; })(img);
reader.readAsDataURL(file);
}
</script>
}
答案 0 :(得分:0)
我发现了问题。我不知道为什么我用了很多时间来发现这个简单的问题。 无论如何,我在这里写的是为了将来需要它的人。 问题是,在读取器将数据设置为图像后,未设置裁剪器。要做到这一点,我们应该有这样的事情。
reader.onloadend = (function () {
$('#image').cropper({
aspectRatio: 16 / 9,
viewMode: 1,
autoCropArea: 1,
minContainerWidth: 700,
minContainerHeight: 600
});
});