我正在尝试使用Jcrop裁剪上传的图像。 我的意图是在用户上传时对图像进行裁剪。 我不需要将用户上传的图像存储在服务器上。 但是,我应该仅将用户通过Jcrop选择的图像部分存储到服务器上。 这是有关问题的Fiddle链接。
我使用了以下代码:
HTML:
<form id="form1">
<input type='file' id="imgInp" />
<img id="blah" class="crop" src="#" alt="your image" />
<input type="hidden" id="x" name="x" />
<input type="hidden" id="y" name="y" />
<input type="hidden" id="w" name="w" />
<input type="hidden" id="h" name="h" />
</form>
CSS:
<style>
#blah {
background-color: #FFF;
width: 500px;
height: 330px;
font-size: 24px;
display: block;
}
</style>
Js:
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#imgInp").change(function(){
console.log(this);
readURL(this);
$(function(){
$('.crop').Jcrop({
onSelect: updateCoords,
bgOpacity: .4,
setSelect: [ 100, 100, 50, 50 ],
aspectRatio: 16 / 9
});
});
});
function updateCoords(c)
{
console.log(c);
$('#x').val(c.x);
$('#y').val(c.y);
$('#w').val(c.w);
$('#h').val(c.h);
};
我尝试上传图片后,将相同的图片用于JCrop,以便获取坐标值生成其余部分的图片。 我现在的问题是:上传后,在图像位置上得到黑色而不是上传的图像。 有人能看一下并找出代码错误在哪里吗?