gpt4 book ai didi

javascript - 图像区域选择 : preselect biggest thumbnail possible respecting aspectRatio

转载 作者:行者123 更新时间:2023-12-01 02:21:45 25 4
gpt4 key购买 nike

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

$('#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)

最佳答案

试试这个代码

        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 overflwoaspectRatio > 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
});

关于javascript - 图像区域选择 : preselect biggest thumbnail possible respecting aspectRatio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15990847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com