简体   繁体   English

如何按比例缩小 <img> WxH中的较短者设置为固定数字?

[英]How can I proportionally scale down an <img> element with the shorter of its WxH set to a fixed number?

html: HTML:

<div id="thumbnail">
  <img src="xxx">
</div>

css: CSS:

div.thumbnail
{
border: 2px solid #ccc;
width: 50px;
height: 50px;
overflow: hidden;
}

Say the image size is greater than 50x50, is there any way that I can proportionally scale down the image so that the shorter of its width and height would become 50px? 假设图片尺寸大于50x50,是否有什么方法可以按比例缩小图片,以使其宽度和高度中的较短者变为50px? Note that the image can be in either portrait or landscape. 请注意,图像可以是纵向或横向。

Divide the width of the image by the height, that's your ratio. 用图像的宽度除以高度,即您的比例。 Then find what's the largest dimension, if it's the width, set the width = 50 * ratio, and height = 50; 然后找到最大的尺寸是什么,如果是宽度,则设置width = 50 * ratio,height = 50; if it's the height set it height = 50 / ratio and the width = 50. Do you need Javascript code? 如果是高度,则将其设置为height = 50 / ratio和width =50。是否需要Javascript代码?

First load up the image in javascript to get its real dimensions. 首先在javascript中加载图片以获取其实际尺寸。

var img = new Image('image.jpg');
var width = img.width;
var height = img.height;

Then determine which one has the larger height, and adjust them accordingly using the ratios. 然后确定哪一个具有更大的高度,并使用比例相应地进行调整。

if (width <= height) {
    var ratio = width/height;
    var newWidth = 50;
    var newHeight = 50 * ratio;
} else {
    var ratio = height/width;
    var newWidth = 50 * ratio;
    var newHeight = 50;
}

Then insert the image into the DOM using jQuery. 然后使用jQuery将图像插入DOM。

$('#imageContainer').append('<img src="' + img.src + '" style="width:' + newWidth + 'px; height:' + newHeight + 'px;" />');

You can't constrain an image to a fixed width and height rectangle, while maintaining aspect ratio, in CSS alone. 仅在CSS中,您不能在保持宽高比的同时将图像限制为固定的宽度和高度矩形。 If you need to do this, it will be either a JavaScript or server side solution. 如果需要这样做,它将是JavaScript或服务器端解决方案。

If you set just a width, then the height will be set to maintain the aspect ratio, likewise just a height, but this will not force the image to fit into a box since you can't know which is greatest, the width or the height. 如果仅设置宽度,则将设置高度以保持宽高比,同样也只是设置高度,但这不会迫使图像适合盒子,因为您无法知道哪个最大,宽度或高度。

Check out ImageMagick if you'd like something server side, otherwise, consider jQuery for a client side solution. 如果您需要服务器端的东西,请查看ImageMagick,否则,请考虑使用jQuery作为客户端解决方案。 JQuery provides a simple API to let you get the dimensions of any element, which you can then scale programatically. JQuery提供了一个简单的API,可让您获取任何元素的尺寸,然后可以以编程方式对其进行缩放。 Newer version of ImageMagick also provide simple calls which will allow you to fit an image into a rectangle. 较新版本的ImageMagick还提供了简单的调用,使您可以将图像调整为矩形。

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

相关问题 如何根据容器高度按比例缩放多个图像? - How can I make a number of images scale proportionally based on its container height? 如何按比例设置此jQuery动画的持续时间? - How can I set the duration of this jQuery animation proportionally? 如何使用内部宽度按比例缩放我的网页以适应任何显示尺寸? - How can I scale my webpage proportionally using inner width to fit any display size? 如何在 vue 可拖动列表旁边设置固定元素 - How can I set a fixed element alongside vue draggable list 如何确定元素是下拉列表元素还是文本输入元素,给定其ID? - How can I check if an element is a drop down list element or a text input element, given its id? 按比例缩放图像以适合其父 object - scale image to fit its parent object proportionally Raphael js:按比例缩放和平移一组元素 - Raphael js: Scale and translate elements in a set proportionally 如何设置元素的高度与其内容相同? - How can I set element's height the same as its content? 如何在`transform:scale()`之前获取元素的位置(相对于其容器)? - How can I get an element's position (relative to its container) before `transform: scale()`? 如何将固定元素的颜色调整为其背景元素颜色? - How can I adjust the color of a fixed element to its background elements color?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM