Logic check and/or better way?
Hate to be paranoid, but....
I wrote a function that takes the height and width of a full size, uploaded image and returns - allegedly, It seems to anyway - new dimensions with the aspect ratio preserved.
My function seems awkward. Could anyone suggest a better approach?
Thanks!
VB Code:
Function RetainAspectRatio(ByVal imgHeight As Integer, ByVal imgWidth As Integer, ByVal IsItASmallThumb As Boolean) As ArrayList
Dim blnSizeCheck As Boolean = IsItASmallThumb
Dim maxheight As Integer
Dim maxwidth As Integer
If blnSizeCheck Then
maxheight = 93
maxwidth = 125
Else
maxheight = 300
maxwidth = 400
End If
imgHeight = Convert.ToDouble(imgHeight)
imgWidth = Convert.ToDouble(imgWidth)
'Get the new height and width depending on whether the end result will be a small thumb or medium-size image
'image
If (imgWidth > imgHeight) Or (imgWidth = imgHeight) Then
While imgWidth > maxwidth
imgWidth = Math.Round(imgWidth - (imgWidth * 0.01), 0)
imgHeight = Math.Round(imgHeight - (imgHeight * 0.01), 0)
End While
ElseIf imgHeight > imgWidth Then
While (imgHeight > maxheight) Or (imgWidth > maxwidth)
imgWidth = Math.Round(imgWidth - (imgWidth * 0.01), 0)
imgHeight = Math.Round(imgHeight - (imgHeight * 0.01), 0)
End While
End If
'send the new dimensions back in an arraylist
imgWidth = Convert.ToInt32(imgWidth)
imgHeight = Convert.ToInt32(imgHeight)
Dim arDimensions As New ArrayList
arDimensions.Add(imgWidth)
arDimensions.Add(imgHeight)
Return arDimensions
End Function
Re: Logic check and/or better way?
Well I don't see a reason to dim blnSizeCheck, just use IsItASmallThumb.
Also your converting the height and width to a double and the sticking it back into an integer which converts it straight back to an integer again...
Is there a reason your using an arraylist? You could just use a string array like this;
Code:
Dim arDimensions(1) As string = {imgWidth, imgHeight}
Re: Logic check and/or better way?
I just noticed you also rounding when you subtract 1% which would make using a double pointless anyway. Looking at your if's and while's again I also realized you only really need the second one 'While (imgHeight > maxheight) Or (imgWidth > maxwidth)' as both need to resolve too true before it'll exit the loop and thus will keep scaling down untill both are withing the size limit
Re: Logic check and/or better way?
ug that is not the way to do it (if I get what you are trying to do)
I dont have time to test this code but you need to work out the second dimension (depending on if height>width or width>height) based on a scale factor
Code:
dim scale as double
If (imgWidth >= imgHeight) Then
'need to calculate the percent of the width to maxwidth
scale= maxwidth/imgwidth
Else ' If imgHeight > imgWidth Then (dont need the logic check)
scale= maxheight/imgHeight
end if
imgWidth = imgwidth * scale '(or imgwidth *= scale)
imgHeight = imgheight * scale
Summat like that anyway.
also this line:
If (imgWidth > imgHeight) Or (imgWidth = imgHeight) Then
is the same as:
If (imgWidth >= imgHeight) Then
Hope that helps
GaZ
Re: Logic check and/or better way?
Guys
Sorry - been out, but thank you for your replies. I sniffed around, and ended up with this
VB Code:
If imgWidth > maxwidth Or imgHeight > maxheight Then
If (maxheight / imgHeight) > (maxwidth / imgWidth) Then
scale = maxheight / imgHeight
Else
scale = maxwidth / imgWidth
End If
If imgWidth > maxwidth Then
scale = maxwidth / imgWidth
imgWidth *= scale
imgHeight *= scale
End If
If imgHeight > maxheight Then
imgWidth /= scale
imgHeight /= scale
scalefactor = maxheight / imgHeight
imgWidth *= scale
imgHeight *= scale
End If
End If
TPM - right, dumped the arraylist. Kept the boolean. This generates dimensions for one of 2 sizes of saved images, depending on the T or F.
Thanks again :)
Dawg