gpt4 book ai didi

javascript - 如何使用裁剪插件避免透明背景

转载 作者:行者123 更新时间:2023-11-29 14:48:33 28 4
gpt4 key购买 nike

我正在使用这个裁剪工具 https://github.com/fengyuanchen/cropper/ .我有这个问题,如果我动态添加图像,图像周围会有一些透明背景。所以图像不适合容器,它也可以在图像周围裁剪。我按照文档中的示例尝试摆脱透明背景,但没有成功。

这是我的代码:

<div id="imgWrap" style="max-height:400px;min-height:400px">
<img id="img" src="" /> // Image gets added dynamically
</div>

javascript

var reader = new FileReader(); 
reader.onloadend = function () {

var img = $('#imgWrap img');
img.attr('src', reader.result);

img.cropper({
aspectRatio: 1 / 1,
autoCropArea: 0.65,
guides: false,
strict: true,
highlight: false,
responsive:true,
dragCrop: false,
movable: true,
resizable: true,
zoomable: true,
touchDragZoom:true,
rotatable: false,
minCropBoxWidth:105,
minCropBoxHeight:105,
built: function () {
// cropper-container is the element where the image is placed
$('.cropper-container').cropper('setCanvasData', {
left: 0,
top: 0,
width: 700,
height: 700
}
);
},
})

我试过这个:https://github.com/fengyuanchen/cropper#setcanvasdatadata但什么也没发生

你可以在这里看到一个例子:

enter image description here

图片的自然尺寸为1920x1200

这是添加图片后生成的: enter image description here

那么,有没有人建议如何摆脱透明背景并使图像适合容器?

最佳答案

我遇到了完全相同的问题。在 Cropper 文档中,它说要设置 img max-width = 100%。我这样做并解决了问题

https://github.com/fengyuanchen/cropper

/* Limit image width to avoid overflow the container */
img {
max-width: 100%; /* This rule is very important, please do not ignore this! */
}

关于javascript - 如何使用裁剪插件避免透明背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29513396/

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