[英]How to calculate width and height values of an image when resized?
I need a way to calculate the width and height values of an image when resized to 1024px 当调整大小为1024px时,我需要一种方法来计算图像的宽度和高度值
The largest value of the image, height or width will be resized to 1024px and I need to figure out the remaining width or height value. 图像,高度或宽度的最大值将调整为1024px,我需要计算剩余的宽度或高度值。
An image (3200 x 2400px) converts to (1024 x 768px) when resized. 调整大小后,图像(3200 x 2400px)将转换为(1024 x 768像素)。
This needs to be dynamic as some images will be portrait and some landscape. 这需要是动态的,因为一些图像将是肖像和一些风景。
Can anyone suggest how I'd work a solution into the following: 任何人都可以建议我如何解决以下问题:
<msxsl:script language="C#" implements-prefix="emint">
<![CDATA[public string GetExtension(string fileName)
{
string[] terms = fileName.Split('.');
if (terms.Length <= 0)
{
return string.Empty;
}
return terms[terms.Length -1];
}
public string GetFileName(string fileName)
{
string[] terms = fileName.Split('/');
if (terms.Length <= 0)
{
return string.Empty;
}
return terms[terms.Length -1];
}
public string GetFileSize(Decimal mbs)
{
Decimal result = Decimal.Round(mbs, 2);
if (result == 0)
{
result = mbs * 1024;
return Decimal.Round(result, 2).ToString() + " KB";
}
return result.ToString() + " MB";
}
public string GetCentimeters(Decimal pix)
{
Decimal formula = (decimal)0.026458333;
Decimal result = pix * formula;
return Decimal.Round(result,0).ToString();
}]]>
</msxsl:script>
width = 1024;
height = 768;
ratio_orig = width_orig/height_orig;
if (width/height > ratio_orig) {
width = height*ratio_orig;
} else {
height = width/ratio_orig;
}
The values of width
and height
at the end correspond to the width and height of the image. 末端的width
和height
值对应于图像的宽度和高度。 This maintains the aspect ratio. 这保持了纵横比。
Here's an algorithm in pseudocode. 这是伪代码的算法。 It will choose the largest possible size (less than 1024x1024px) that has the same width/height ratio as the original image. 它将选择与原始图像具有相同宽度/高度比的最大可能尺寸(小于1024x1024px)。
target_width = 1024
target_height = 1024
target_ratio = target_width / target_height
orig_ratio = orig_width / orig_height
if orig_ratio < target_ratio
# Limited by height
target_width = round(target_height * orig_ratio)
else
# Limited by width
target_height = round(target_width / orig_ratio)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.