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

标签 php jquery image-processing jquery-plugins

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

该插件提供了两个选项minCropBoxWidthminCropBoxHeight,但是它们仅控制屏幕上的实际裁剪框。由于裁剪器内图像的大小可以是任何大小(在合理范围内),这无助于确保最终输出的大小。它足够简单,可以检索图像的实际大小(它在 data 参数中传递给crop 函数)。我所坚持的是,一旦满足最小宽度/高度值,就阻止裁剪框缩小尺寸。我得到 $(this).cropper(...).disable 不是一个函数

$('.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。为了防止事件完成,您可以返回一个错误值。

这是一个例子:

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

关于php - fengyuanchen jQuery 裁剪插件 - 最小裁剪验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30051695/

相关文章:

javascript - 在 Firefox 扩展中使用 Jstree 插件时出错

java - OpenCV - 拉普拉斯滤波器中的参数 (Java)

PHP PDO ODBC 连接

php - 寻找积极维护的 php 矩阵数学库

php - 如何使用 Ajax 和 jQuery 更改 Blade 表中检索到的数据顺序

jquery - css hover div,在内联样式背景图像上叠加透明图像

php - 在 Window XAMPP 中的本地系统中设置 Yii2 项目出现错误

php - 在 Javascript If else 语句中显示 HTML

matlab - 在 MATLAB 中使用 imgradient 时如何解释梯度的方向?

python - 如何使用 python 查找图像中圆的平均 RGB 值?