【问题标题】:fengyuanchen jQuery cropper plugin - minimum crop validationfengyuanchen jQuery 裁剪器插件 - 最小裁剪验证
【发布时间】:2015-07-15 02:40:52
【问题描述】:

我正在尝试使用 fengyuanchen jquery cropper 插件 - https://github.com/fengyuanchen/cropper 强制裁剪数据输出的最小大小

插件提供minCropBoxWidthminCropBoxHeight 两个选项,但这些选项仅控制屏幕上的实际裁剪框。由于裁剪器中的图像大小可以是任意的(在合理范围内),这无助于确保最终输出的大小。它可以直接检索图像的实际大小(它在数据参数中传递给crop 函数)。我坚持的是,一旦满足最小宽度/高度值,就停止裁剪框的尺寸减小。我得到$(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);
                        }
                    }

【问题讨论】:

    标签: php jquery image-processing jquery-plugins


    【解决方案1】:

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

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

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

    这是一个例子:

    $('.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

    【讨论】:

    • 非常感谢您的帮助
    • 有效,但有丑陋或错误的行为(例如:如果您拖动,缩小cropBox,非常快,cropBox 将小于所需的 minWidth)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 2016-03-18
    相关资源
    最近更新 更多