fengyuanchen jQuery 裁剪插件 - 最小裁剪验证

fengyuanchen jQuery cropper plugin - minimum crop validation

我正在尝试使用 fengyuanchen jquery 裁剪插件 - https://github.com/fengyuanchen/cropper

强加裁剪数据输出的最小尺寸

该插件提供了两个选项 minCropBoxWidthminCropBoxHeight,但是这些选项仅控制屏幕上的实际裁剪框。由于裁剪器中图像的大小可以是任意大小(在合理范围内),这无助于确保最终输出的大小。它足以直接检索图像的实际大小(它在数据参数中传递给 crop 函数)。我坚持的是在满足最小 width/height 值后停止缩小裁剪框的大小。我得到 $(this).cropper(...).disable is not a function

$('.image-preview img').cropper({
                    aspectRatio:1/1,
                    strict:true,
                    background:false,
                    guides:false,
                    autoCropArea:1,
                    rotatable:false,
                    minCropBoxWidth:20,//using these just to stop box collapsing on itself
                    minCropBoxHeight:20,
                    crop:function(data){
                        //test the new height/width
                        if(data.height < 120 || data.width < 120){
                            //try to make it stop 
                            $(this).cropper().disable(); //here be the error
                        }else{
                           var json = [
                              '{"x":' + data.x,
                              '"y":' + data.y,
                              '"height":' + data.height,
                              '"width":' + data.width + '}'
                            ].join();
                           $('#image-data').val(json);
                        }
                    }

首先,调用disable方法是这样的:

$(this).cropper('disable');

但这对您想要实现的目标没有帮助。 相反,我建议处理由裁剪器触发的适当事件:dragstart.cropperdragmove.cropper。为防止事件结束,您可以 return 一个假值。

这是一个例子:

$('.img-container img').on('dragmove.cropper', function (e) {
    console.log('dragmove.cropper');

    var $cropper = $(e.target);

    // Call getData() or getImageData() or getCanvasData() or
    // whatever fits your needs
    var data = $cropper.cropper('getCropBoxData');

    console.log("data = %o", data);

    // Analyze the result
    if (data.height <= 150 || data.width <= 150) {
        console.log("Minimum size reached!");

        // Stop resize
        return false;
    }

    // Continue resize
    return true;
}).on('dragstart.cropper', function (e) {
    console.log('dragstart.cropper');

    var $cropper = $(e.target);

    // Get the same data as above 
    var data = $cropper.cropper('getCropBoxData');

    // Modify the dimensions to quit from disabled mode
    if (data.height <= 150 || data.width <= 150) {
        data.width = 151;
        data.height = 151;

        $(e.target).cropper('setCropBoxData', data);
    }
});

JSFiddle