Reputation: 891
I need a way to calculate the width and height values of an image when resized to 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.
An image (3200 x 2400px) converts to (1024 x 768px) when resized.
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>
Upvotes: 0
Views: 2109
Reputation: 32072
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.
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)
Upvotes: 0
Reputation: 10057
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. This maintains the aspect ratio.
Upvotes: 2