按区域调整图像大小
我正在尝试编写一个javascript函数来根据给定区域(或者在我的情况下(有点不准确)“平均尺寸”)调整图像大小,因为这更容易思考。我想要的不是输入最大高度和宽度,而是提供最大区域,以便长或窄的图像在视觉上看起来大小大致相同
I。我真的越来越不过,我明白了它的数学方面......只是如何逻辑它,因为我最近没有做太多数学工作,
基本上,给定长宽比,我想确定一个区域内的最大尺寸
:
function resizeImgByArea(img, avgDimension){
var w = $(img).width();
var h = $(img).height();
var ratio = w/h;
var area = avgDimension * avgDimension;
var targetHeight //something involving ratio and area
var targetWidth //something involving ratio and area
$(img).width(targetWidth);
$(img).height(targetHeight);
}
不确定这是否是这里的主题,但我无法思考。
I am trying to write a javascript function to resize an image based on a given area (or in my case (somewhat inaccurate) 'average dimension' since that's easier to think in terms of. Rather than feeding in maximum height and width, I want to feed in maximum area so that long or narrow images will appear visually to be roughly the same size.
I'm getting really caught on the math aspect of it, though... just how to logic it, as I haven't done much math of late.
Basically, given an aspect ratio I want to determine the maximum size within an area.
Something like this:
function resizeImgByArea(img, avgDimension){
var w = $(img).width();
var h = $(img).height();
var ratio = w/h;
var area = avgDimension * avgDimension;
var targetHeight //something involving ratio and area
var targetWidth //something involving ratio and area
$(img).width(targetWidth);
$(img).height(targetHeight);
}
Not sure if this is on topic here, but I'm not able to brain it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
听起来您想将缩略图的像素限制为尽可能接近所有其他缩略图的平均区域,对吗?
基本上,给定原始图像的高/宽和目标区域 A:
计算新缩略图的 x/y 大小:
如果我们对缩略图大小进行四舍五入,我们将得到 86x114 = 9,804 像素
。 ..要将 640x480 图像转换为标准的 10,000 左右像素大小,您需要一个高度为 86-87 、高度为 114-116 的新图像大小 宽度。
Sounds like you want to constrain the thumbnail's pixels to be as close as possible to the average area as all the other thumbnails, right?
So basically, given the h/w of the original image, and a target area A:
To calculate the new thumbnail's x/y size:
if we'd rounded down on the thumbnail sizes, we'd get 86x114 = 9,804 pixels
so... to convert your 640x480 image to a standard 10,000-ish pixel size, you need a new image size of 86-87 height and 114-116 width.
您是否正在寻找类似的东西:
Are you looking for something like:
这并不难。
maxPix =average^2
maxPix = x * h + x *
waverage^2 = x*h + x*w //: xaverage
^2/x = h+w
逆并乘以average^2
x =average^2 / (h+w)
然后将 h 和 w 与 x 相乘以获得新的尺寸
It isn't that hard.
maxPix = average^2
maxPix = x * h + x * w
average^2 = x*h + x*w //: x
average^2/x = h+w
inverse and multiply with average^2
x = average^2 / (h+w)
then multiply h and w with x to get the new dimensions
这是我想出的函数,它比提到的一些函数更简单,并且可以满足我的需要。由于我使用的特定要求,它限制了设置的 maxWidth,但不限制高度。. 可能适合设置 maxHeight 以及一些清理,但它已经完成了。
Here is the function I came up with that's simpler than some mentioned and does what I need. It constrains to a set maxWidth, but not height because of the particular requirements I was using.. it would probly be appropriate to throw on a maxHeight as well as well as some cleanup, but it gets 'er done.
提供图像或具有
width
和height
属性的任何内容作为参数。area
参数也假定width
和height
属性。Give an image or anything with
width
andheight
properties as an argument.area
argument assumewidth
andheight
properties too.