gpt4 book ai didi

javascript - 如何使用cropper.js对图像进行裁剪?

转载 作者:行者123 更新时间:2023-12-01 05:11:35 24 4
gpt4 key购买 nike

我正在尝试了解如何使用cropper.js,到目前为止它看起来不错,但是......当我尝试裁剪图像时,如何将结果放回到我的表单中?

我的输入有 ID“profile_avatar”。所以我尝试将更新的图像放在那里,但不能。

$('#cut_button').click(function(e) {
e.preventDefault();
var croppedImageDataURL = cropper.getCroppedCanvas()
$('#profile_avatar').val(croppedImageDataURL.toDataURL("image/png"))
});

但是当我点击“剪切按钮”时,我得到了

InvalidStateError:尝试使用不可用或不再可用的对象

在控制台中。我究竟做错了什么?我正试图在几个小时内击败它。非常感谢!

带 View 的超薄文件:

  = form_for @profile do |f|
= f.label :avatar
= f.file_field :avatar
.canvas_window
canvas id='canvas'
.preview
= link_to('Cut', '', id: 'cut_button')
= f.label :photos
= f.file_field :photos, multiple: true
= f.label :description
= f.text_area :description
= @profile.errors.messages[:description].presence
= hidden_field :dimensions, ''
= f.submit I18n.t('user.forms.buttons.submit')

javascript:
var canvas = $("#canvas"),
context = canvas.get(0).getContext("2d"),
$result = $('#result');

$('#profile_avatar').on( 'change', function(){
console.log('start')
if (this.files && this.files[0]) {
if ( this.files[0].type.match(/^image\//) ) {
var reader = new FileReader();
reader.onload = function(evt) {
var img = new Image();
img.onload = function() {
context.canvas.height = img.height;
context.canvas.width = img.width;
context.drawImage(img, 0, 0);
const image = document.getElementById('canvas');
const cropper = new Cropper(image, {
preview: '.preview',
aspectRatio: 16 / 9,
crop(event) {
dimensions = event.detail.width + 'x' + event.detail.width + '+' + event.detail.x + '+' + event.detail.y
$('#new_profile input#dimensions_').val(dimensions)
console.log(dimensions);
console.log(event.detail.x);
console.log(event.detail.y);
console.log(event.detail.width);
console.log(event.detail.height);
},
});

$('#cut_button').click(function(e) {
e.preventDefault();
var croppedImageDataURL = cropper.getCroppedCanvas()
HERE IS ERROR \/
$('#profile_avatar').val(croppedImageDataURL.toDataURL("image/png"))
console.log(croppedImageDataURL.toDataURL("image/png"))
});
};
img.src = evt.target.result;
};
reader.readAsDataURL(this.files[0]);
}
else {
alert("Invalid file type! Please select an image file.");
}
}
else {
alert('No file(s) selected.');
}
});

html:

<form class="new_profile" id="new_profile" enctype="multipart/form-data" action="/profiles" accept-charset="UTF-8" method="post">
<input type="hidden" name="authenticity_token" value="j53mqq1th1kb17ynj2jxIMcPFRC210EQVLBSgEu2n4FLOONtFBH3Vu7wrjc+iDrogn99H/emvN5qUdyZo2pAkg==">
<label for="profile_avatar">Avatar</label>
<input type="file" name="profile[avatar]" id="profile_avatar">
<div class="canvas_window">
<canvas id="canvas"></canvas>
</div>
<div class="preview"></div>
<a id="cut_button" href="">Cut</a>
<label for="profile_photos">Photos</label>
<input multiple="multiple" type="file" name="profile[photos][]" id="profile_photos">
<label for="profile_description">Description</label>
<textarea name="profile[description]" id="profile_description"></textarea>
<input type="hidden" name="dimensions[]" id="dimensions_">
<input type="submit" name="commit" value="Submit" data-disable-with="Submit">
</form>

最佳答案

值得注意的一件事是,您需要将图像的源设置为 toDataUrl() 的输出。例如:

HTML:

<img id="cropped_image" src="/blank.gif"></img>

JS:

$("#cropped_image").attr("src", croppedImageDataURL.toDataURL("image/png"));

您可以使用 JavaScript 即时创建该图像或包含 <img/>在标记中使用空白图像或隐藏标记(添加裁剪图像后显示它)。

要在表单中传递图像,您应该使用输入字段,因为此时的值是一个字符串(例如“data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNby”)。

HTML:

<input id="cropped_image" type="hidden" />

JS:

$("#cropped_image").val(croppedImageDataURL.toDataURL("image/png"));

关于javascript - 如何使用cropper.js对图像进行裁剪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61953435/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com