简体   繁体   English

缩放图像以最适合帧的算法(div)

[英]algorithm to scale an image to best fit a frame (div)

I am very weak in Math. 我的数学很弱。 I am a web programmer, and usually my work does not involve too much math - its more of putting records into database, pulling out reports, making those fancy web pages etc etc. So, I got a task that is "mathy" for me, but I am hoping its child's play for some of you Math guys out there, and you will help me out...It might even be an interesting problem for you, its just too complex for me. 我是一名Web程序员,通常我的工作不会涉及太多的数学运算-它更多地是将记录放入数据库,提取报告,制作那些精美的网页等。因此,我得到的任务对我来说是“数学” ,但我希望它能为你们中的某些数学家带来乐趣,您会帮助我...对于您来说,这甚至可能是一个有趣的问题,对我来说太复杂了。

So recently I got this task, where I am given an image with an area marked on top of it. 因此,最近我完成了这项任务,在该任务中,我得到了一个图像,该图像的顶部标有一个区域。 This area would be marked as top left pixels (x1,y1) and bottom right pixels (x2,y2) So for the image I have the following information: 该区域将被标记为左上像素(x1,y1)和右下像素(x2,y2),因此对于图像,我具有以下信息:

Image Width (iw)
Image Height (ih)
Image Area Top Left (x1,y1)
Image Area Bottom Right (x2,y2)

Now there are a collection of frames, each frame will have Frame Width (fw) and Frame Height (fh) . 现在有一个框架集合,每个框架都有Frame Width (fw)Frame Height (fh) My job is to find optimal way to fit Image Area inside frame without having any blank area inside frame, using image scaling if required. 我的工作是找到适合的方法来在框架内适合图像区域,而在框架内没有任何空白区域,如果需要,可以使用图像缩放。

So I broke it down to following tasks: 因此,我将其分解为以下任务:

  1. Image Area (not fill image) has to fit inside Frame. 图像区域(非填充图像)必须适合框架。 If Image Area > Frame , I should scale Image down (to a %). 如果Image Area > Frame ,我应该将图像缩小(缩小到%)。 If Image Area < Frame I should scale Image Up. 如果Image Area < Frame ,则应放大图像。

  2. The aspect ratio of the image should not be disturbed when I scale. 缩放时不应干扰图像的纵横比。

  3. I should scale Image based on the longer side of Image Area to make sure that whole Image Area comes inside Frame. 我应该根据“图像区域”的较长边缩放图像,以确保整个“图像区域”都位于“框架”内。 However when scaling Image down, sometimes this may cause the other side to have blank space inside Frame because of extreme aspect ratios. 但是,当按比例缩小图像时,由于极高的宽高比,有时这可能会导致另一侧在Frame内部留有空白。 In such case, I should only scale down until one side hits Image Height or Image Width. 在这种情况下,我只应缩小比例直到一侧达到“图像高度”或“图像宽度”。 This means that the Image Area is not fully shown inside the Frame, but that is fine because the first priority is to not have blank space inside Frame. 这意味着图像区域没有在框架内完全显示,但这很好,因为第一优先级是框架内没有空格。

Now that I have written all these rules, I am unable to wrap my head around formalizing this into a mathematical "formula"/"procedure"/"rule-set" that I can then recreate in my software. 现在,我已经编写了所有这些规则,现在我无法将其形式化为数学的“公式” /“过程” /“规则集”,然后可以在软件中重新创建。

For scaling, I found that this formula works well (in programming pseudo-code): 对于缩放,我发现此公式很好用(在编程伪代码中):

imageX = Image.width()
imageY = Image.height();
imageRatio = imageX / imageY;

frameX = Frame.width();
frameY = Frame.height();
frameRatio = frameX / frameY;

if (imageRatio < wellRatio) 
{
    scale = imageX / wellX;
} 
else 
{
    scale = imageY / wellY;
}
resizeX = (imageX / scale);
resizeY = (imageY / scale);

However I do not know how to tie this into the whole picture. 但是,我不知道如何将其结合到整个图片中。

Can anyone help me please? 谁能帮我吗? I have been trying unsuccessfully for over a week now :( I am using PHP and Javascript and I am pretty well versed with these two technologies. Usually I can solve most problems that I encounter day-to-day which involves them, however this is way over my head. 我已经尝试了一个多星期,但都没有成功:(我正在使用PHP和Javascript,并且我对这两种技术都非常精通。通常,我可以解决我所遇到的涉及它们的大多数问题,但这是在我头上

It sounds like the priority is to fill the frame first so you don't have letterboxing for any image. 听起来优先级高是首先填充框架,这样您就不会在任何图像上出现黑框。

The best way to do this is to look for the shorter side and use CSS to stretch that side to fill the frame. 最好的方法是查找较短的一侧,然后使用CSS拉伸该侧以填充框架。 This will preserve the aspect ratio, but might cut off part of the image. 这将保留长宽比,但可能会切掉部分图像。

$('img').css(this.width > this.height ? 'height' : 'width', '100%');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM