如何按比例调整图像大小/保持纵横比?
我的图像尺寸相当大,我想用 jQuery 缩小它们,同时保持比例受限,即相同的纵横比。
有人可以给我指出一些代码,或者解释一下逻辑吗?
I have images that will be quite big in dimension and I want to shrink them down with jQuery while keeping the proportions constrained, i.e. the same aspect ratio.
Can someone point me to some code, or explain the logic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
我认为这确实是很酷的方法:
I think this is a really cool method:
看一下 http://ericjuden.com/2009/07 中的这段代码/jquery-image-resize/
Have a look at this piece of code from http://ericjuden.com/2009/07/jquery-image-resize/
如果我正确理解这个问题,你甚至不需要 jQuery。在客户端按比例缩小图像可以单独使用 CSS 来完成:只需将其
max-width
和max-height
设置为100%
即可。这是小提琴: http://jsfiddle.net/9EQ5c/
If I understand the question correctly, you don't even need jQuery for this. Shrinking the image proportionally on the client can be done with CSS alone: just set its
max-width
andmax-height
to100%
.Here's the fiddle: http://jsfiddle.net/9EQ5c/
为了确定宽高比,您需要有一个目标比例。
在此示例中,我使用
16:10
,因为这是典型的显示器宽高比。上面的结果将是
147
和300
In order to determine the aspect ratio, you need to have a ratio to aim for.
In this example I use
16:10
since this the typical monitor aspect ratio.Results from above would be
147
and300
这个问题有4个参数
并且有3个不同的条件参数
解决方案
这就是您需要做的。
这是一个成熟的论坛,我不会给你代码:)
There are 4 parameters to this problem
And there are 3 different conditional parameters
solution
that's all you need to do.
This is a mature forum, I am not giving you code for that :)
实际上我刚刚遇到了这个问题,我发现的解决方案非常简单和奇怪
,并且奇迹般地将图像大小调整到新的高度并保持相同的比例!
actually i have just run into this problem and the solution I found was strangely simple and weird
and miraculously the image is resized to the new height and conserving the same ratio!
是否
有帮助吗?
浏览器将负责保持纵横比完整。
即当图像宽度大于高度时,
max-width
启动,并且其高度将按比例计算。同样,当高度大于宽度时,max-height
将生效。为此,您不需要任何 jQuery 或 javascript。
受 ie7+ 和其他浏览器支持 (http://caniuse.com/minmaxwh)。
Does
<img src="/path/to/pic.jpg" style="max-width:XXXpx; max-height:YYYpx;" >
help?Browser will take care of keeping aspect ratio intact.
i.e
max-width
kicks in when image width is greater than height and its height will be calculated proportionally. Similarlymax-height
will be in effect when height is greater than width.You don't need any jQuery or javascript for this.
Supported by ie7+ and other browsers (http://caniuse.com/minmaxwh).
如果图像是成比例的,那么此代码将用图像填充包装器。如果图像不成比例,则额外的宽度/高度将被裁剪。
If the image is proportionate then this code will fill the wrapper with image. If image is not in proportion then extra width/height will get cropped.
没有额外的临时变量或括号。
请记住:如果宽度和高度属性不适合图像,搜索引擎不喜欢它,但他们不懂 JS。
Without additional temp-vars or brackets.
Keep in Mind: Search engines don't like it, if width and height attribute does not fit the image, but they don't know JS.
这应该适用于所有可能比例的图像
This should work for images with all possible proportions
这是对梅迪威答案的更正。新的宽度和/或高度未设置为最大值。一个好的测试用例如下(1768 x 1075 像素): http: //spacecoastsports.com/wp-content/uploads/2014/06/sportsballs1.png。 (由于缺乏声望点,我无法在上面发表评论。)
Here's a correction to Mehdiway's answer. The new width and/or height were not being set to the max value. A good test case is the following (1768 x 1075 pixels): http://spacecoastsports.com/wp-content/uploads/2014/06/sportsballs1.png. (I wasn't able to comment on it above due to lack of reputation points.)
2个步骤:
步骤1)计算Image的原始宽度/原始高度的比率。
步骤2)将original_width/original_height比率乘以新的所需高度,得到与新高度对应的新宽度。
2 Steps:
Step 1) calculate the ratio of the original width / original height of Image.
Step 2) multiply the original_width/original_height ratio by the new desired height to get the new width corresponding to the new height.
经过一番尝试和错误后,我得出了这个解决方案:
After some trial and error I came to this solution:
可以使用 CSS 来实现调整大小(保持纵横比)。
这是受丹·达斯卡莱斯库 (Dan Dascalescu) 帖子启发的进一步简化的答案。
http://jsbin.com/viqare
The resize can be achieved(maintaining aspect ratio) using CSS.
This is a further simplified answer inspired by Dan Dascalescu's post.
http://jsbin.com/viqare
如果您想要特定的纵横比,您可以确定宽度高度,
让你拥有3264×2448的图片
图片长宽比为=> 2448 ÷ 3264 =0.75
现在只需检查除法结果为 0.75 的数字。
喜欢:为了
16:9 => 9÷16 =0.5625(错误不是0.75)
现在 4:3 => 3÷4=0.75 (我们明白了)
所以原来的长宽比是4:3
现在调整图像大小即可
宽度=3264 ÷/× 4
身高=2448 ÷/× 3
÷ 减少
× 增加
希望您能够理解并自己编写代码,这是非常有效的,因为我们只需要做非常基本的算术,就这么简单的除法或乘法。
如果我错了请告诉我。
You can determine width height if you want a particular aspect ratio to do so,
Let you have a picture of 3264×2448
Pictures aspect ratio is => 2448 ÷ 3264 =0.75
Now just check number which gives 0.75 on division.
Like: for
16:9 => 9÷16 =0.5625 (wrong it is not 0.75)
Now 4:3 =>3÷4=0.75 (we get it )
So the original aspect ratio is 4:3
now to resize the image just do
Width=3264 ÷/× 4
Height=2448 ÷/× 3
÷ for reducing
× for increasing
Hope you can understand and code yourself this is very effective because we just need to do very basic arithmetic just division or multiplication so simple.
Let me know if i am wrong.
这个问题可以通过CSS来解决。
This issue can be solved by CSS.
调整大小以适合容器,获取比例因子,缩小百分比控制
Resize to fit the container, get scale factor, scale down percentage control
将图像大小调整到一定百分比
Resizing an image to a certain percentage
这对我来说完全适用于可拖动项目-aspectRatio:true
This totally worked for me for a draggable item - aspectRatio:true