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

ST8*_*T80 5 javascript css jquery image crop

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

这是我的代码:

<div id="imgWrap" style="max-height:400px;min-height:400px">
    <img id="img" src="" /> // Image gets added dynamically
</div>
Run Code Online (Sandbox Code Playgroud)

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
           }
        );                                      
    },
})
Run Code Online (Sandbox Code Playgroud)

我试过这个:https : //github.com/fengyuanchen/cropper#setcanvasdatadata但没有任何反应

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

在此处输入图片说明

图片的自然尺寸为 1920x1200

这是添加图像后生成的内容: 在此处输入图片说明

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

mar*_*tin 2

我有完全相同的问题。在 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! */
}
Run Code Online (Sandbox Code Playgroud)