Forum Moderators: open

Message Too Old, No Replies

Resizing an image and keeping the aspect ratio

         

Johny Favourite

9:45 am on Sep 17, 2004 (gmt 0)

10+ Year Member



HI guys,

I'm tearing my hair out trying to write this little script.

Basically What I want to do is display an image with a maxium width of say 80 pixels but keep the ratio of the height.

Is it at all possible?

the thing is I can do it with one file in javascript but I'm putting this in a loop in asp!

musicales

11:34 am on Sep 17, 2004 (gmt 0)

10+ Year Member



can't you just set the width to 80 and leave the height blank?

mattglet

11:45 am on Sep 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's the type of calculation you want to perform:

intWidth = yourCurrentImageWidth
intHeight = yourCurrentImageHeight

if intWidth > intHeight then
intRatio = (yourMaxWidth / intWidth)
else
intRatio = (yourMaxHeight / intHeight)
end if

newWidth = intWidth * intRatio
newHeight = intHeight * intRatio

Johny Favourite

11:48 am on Sep 17, 2004 (gmt 0)

10+ Year Member



Opps, I forgot to mention that if the height gets above a certain value I want to crop it if you know what i mean, I know the calculation i need to do I just need to get the value from the image in the first place.

thanks guys

mattglet

11:51 am on Sep 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The easiest way to do what you're looking for is to use an image component like ASPImage or SoftArtisans' ImgWriter.

Johny Favourite

12:02 pm on Sep 17, 2004 (gmt 0)

10+ Year Member



ok, I'm a bit new to this, what would that involve?

I would guess i need to purchase one of the above?

defanjos

12:04 pm on Sep 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are running your own server, yes. If you are paying a hosting company, then, they might have one of the components installed already - inquire with them.