# Scaling images with aspect ratio below 100%

**URL:** <https://forum.kirupa.com/t/scaling-images-with-aspect-ratio-below-100/315534>\
**Category:** flash\
**Created:** [October 28, 2010, 9:13am UTC](https://forum.kirupa.com/t/scaling-images-with-aspect-ratio-below-100/315534 "2010-10-28T09:13:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![iloveaphextwin](https://avatars.discourse-cdn.com/v4/letter/i/e274bd/32.png) [@iloveaphextwin](https://forum.kirupa.com/u/iloveaphextwin)\
**Post date:** [October 28, 2010, 9:13am UTC](https://forum.kirupa.com/t/scaling-images-with-aspect-ratio-below-100/315534/1 "2010-10-28T09:13:12Z")

</div>

Hi there,

I have searched this forum and online but can’t find a solution to the problem I am having.

I need to scale images so that they always fit within my browser but also retain their aspect ratio. All other examples I have seen at some point either the width or height is cropped off by the edge of the browser. I need my images to always stay inside but also I do not want them to ever scale above 100% i.e. no enlargement.

I have some code that is pretty bad. It works but causes a massive bug where the image goes crazy.

[http://www.ourtestsite.org/test/cp\_test/](http://www.ourtestsite.org/test/cp_test/)

I found this one already  
[http://www.yapiodesign.com/blog/2007/02/11/scale-image-proportionally/](http://www.yapiodesign.com/blog/2007/02/11/scale-image-proportionally/)

however the images don’t stay within 100% scale and also get cropped off.

Any help is really appreciated and I will try and help out anyone else as much as I can.

The code I have is like this

```auto

if(mainTL.allImages._width > Stage.width){
	mainTL.allImages._width = Stage.width;
	mainTL.allImages._height = mainTL.allImages._width * imageRatio;
} else if(mainTL.allImages._height > Stage.height){
	mainTL.allImages._height = Stage.height;
	mainTL.allImages._width = mainTL.allImages._height * imageRatio;
} else if(mainTL.allImages._width < Stage.width){
	mainTL.allImages._width = Stage.width;
	mainTL.allImages._height = mainTL.allImages._width * imageRatio;
} else if(mainTL.allImages._height < Stage.height){
	mainTL.allImages._height = Stage.height;
	mainTL.allImages._width = mainTL.allImages._height * imageRatio;
}

```

Thanks

Will
