|
-
Jun 28th, 2006, 02:06 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Size Constraints ASAP!
I have images that I want to display on a web page. I have user defined width and height limitations. If the original width of the image is > the user defined width or the original height of the image is > the user defined height then I need to shrink the image. The problem is that I need to shrink the image and maintain the aspect ratio. What is the formula to "fit" an image into a designated space while keeping its aspect ratio.
For example if MaxWidth = 10 and MaxHeight = 5 and the image is 12 x 6, I would want the image to be 10 x 5.
For example if MaxWidth = 10 and MaxHeight = 5 and the image is 6 x 12, I would want the image to be 2.5 x 5.
Keep in mind there are cases where the one dimension may fit, but then the other dimension is still too large which would require the image to be made smaller.
PLEASE HELP!!
-
Jun 28th, 2006, 02:50 PM
#2
Re: Size Constraints ASAP!
Think about what you are doing. How do you know which one you want to be the defining dimension? What you would do is scale the width in order to match the height available. If it is smaller than the allowed width then you have the answer. If it is greater than the allowed width, then you need to scale the other way.
VB Code:
NewWidth = OldWidth x NewAllowedHeight/OldHeight
If NewWidth <= NewAllowedWidth Then
NewWidth = NewWidth
NewHeight = NewAllowedHeight
Else
NewHeight = OldHeight x NewAllowedWidth/OldWidth
NewWidth = NewAllowedWidth
End If
That's it.
zaza
-
Jun 28th, 2006, 02:55 PM
#3
Thread Starter
Fanatic Member
Re: Size Constraints ASAP!
Yeah I had an algorithm going, but I was off by just a bit. Here's what I am going with:
VB Code:
Dim dOriginalRatio As Double
Dim dDestinationRatio As Double
'get the original ratio
dOriginalRatio = lOriginalWidth / lOriginalHeight
'get the destination ratio
dDestinationRatio = lNewWidth / lNewHeight
'calculate destination width
lDestinationWidth = IIf(dDestinationRatio < dOriginalRatio, lNewWidth, lNewHeight * dOriginalRatio)
'calculate destination height
lDestinationHeight = IIf(dDestinationRatio < dOriginalRatio, lNewWidth / dOriginalRatio, lNewHeight)
Thanks for your reply!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|