【问题标题】:ImageAreaSelect: preselect biggest thumbnail possible respecting aspectRatioImageAreaSelect:根据 aspectRatio 预选最大的缩略图
【发布时间】:2013-04-06 03:08:48
【问题描述】:

这是我第一次尝试的方式:

$('#thumbnail').imgAreaSelect({ 
    x1: 5,
    y1: 5,
    x2: $('#thumbnail').width(),
    y2: $('#thumbnail').width()*0.66,
    aspectRatio: '1:0.66'
});

但一些裁剪后的图像不会溢出...

这似乎对我尝试过的大多数图像分辨率进行了预选...

var thwidth = $('#thumbnail').width();
var thheight = $('#thumbnail').height();
aspectRatio = 0.66;

$('#thumbnail').imgAreaSelect({ 
    x1: 5,
    y1: 5,
    x2: thwidth - 80,
    y2: (thwidth - 80) * aspectRatio,
    aspectRatio: '1:0.66'
});

但它不会选择可能的最大值;而且我觉得有点脏……

如何选择最大的(如果可能,居中)宽度/高度坐标尊重纵横比? (在这种情况下:1:0.66)

【问题讨论】:

    标签: javascript jquery jquery-plugins crop aspect-ratio


    【解决方案1】:

    试试这个代码

            var selWidth = 500;
    
            var photo = $('#photo'),
               photoWidth = parseInt($('#photo').width()),
               maxWidth = Math.min(selWidth, photoWidth)
               aspectRatio = 0.66,
               maxHeight = maxWidth * aspectRatio,
               yTop = parseInt(photo.height()) / 2 - maxHeight / 2;
    
            $('img#photo').imgAreaSelect({
               x1: photoWidth / 2 - maxWidth / 2,
               y1: yTop,
               x2: (photoWidth / 2 - maxWidth / 2) + maxWidth,
               y2: yTop + maxHeight
            });
    

    jsfiddle 此代码创建一个居中选择区域具有给定的纵横比和最大宽度。如果最大宽度超过了图片的宽度,则使用图片的宽度作为最大宽度。请注意,它适用于 jquery 1.8.3,我认为这是因为 imageareaselect 插件与最新的 jquery 版本不兼容(不过我不确定)。


    更新

    我改进了代码以包含 height overflwo 和 aspectRatio > 1 的情况。我希望这适用于所有条件:)

            var selWidth = 350;
    
            var photo = $('#photo'),
               photoWidth = parseInt($('#photo').width()),
               photoHeight = parseInt($('#photo').height()),
               maxWidth = Math.min(selWidth, photoWidth),
               aspectRatio = 0.66,
               maxHeight = maxWidth * aspectRatio;
    
            if (maxHeight > photoHeight) {
               maxHeight = photoHeight;
               maxWidth = maxHeight * ( 1 / aspectRatio);
            }
    
            var yTop = photoHeight / 2 - maxHeight / 2;
    
            $('img#photo').imgAreaSelect({
               x1: photoWidth / 2 - maxWidth / 2,
               y1: yTop,
               x2: (photoWidth / 2 - maxWidth / 2) + maxWidth,
               y2: yTop + maxHeight
            });
    

    【讨论】:

    • 支持最大宽度。好决定。 :) 这个脚本可以在宽高比 > 1 的情况下工作吗? (例如纵向模式拇指)
    • 您好!谢谢你的时间!我将要测试.. 我不明白为什么 var selWidth 是一个常数(而宽度完全代表)?
    • 它应该是图像裁剪区域的最大尺寸。如果图像更大,它只会选择中间的 500px 部分。
    • selWidth 定义选择区域的宽度。如果您将selWidth 设置得太大,它只会选择整个图像的宽度。如果您不想手动设置,只需在您认为合适的地方执行maxWidth = photoWidth。
    • 我明白了,我用我自己最大的可能替换了它。这很好用!谢谢!
    【解决方案2】:

    我准备了这个小fiddle。 我花了比 Ejay 多一点的时间,但我包含了一个视觉演示。

    计算出拇指的位置和宽度后,就可以这么简单了。 (使用小提琴中的变量名)

    $('#thumbnail').imgAreaSelect({ 
        x1: thumbX,
        y1: thumbY,
        x2: thumbX+thumbW,
        y2: thumbY+thumbH,
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-06
      • 2011-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      • 1970-01-01
      相关资源
      最近更新 更多