19 October 2009

Resize function for image or MovieClip with advanced options - Actionscript2 AS2

I found myself often needing to resize images to fit different spaces using actionscript so I wrote this handy function a while back. It allows you to quickly resize the image proportionally to fit your needs and doesn't require much thought. See usage examples below and you'll see what I mean.

Click to enlarge:


The function: (updated 22 June 2010)
(Please see right hand panel before copying) :


//////////////////////////////////////////
/* resizes and returns an object containing the new dimensions of the processed MC 
PARAMS::::::::::::::::::::::
obj       //Movie clip to resize
maxW       //maximum width
maxH       //maximum height
tofit       //resizes proportionally to make the object fit inside specified space 
returnOnly      //returns new size without resizing
disallowScaleUp    //does not resize if image is smaller than target dimensions
USAGE:::::::::::::::::::::::
Resize(obj,300,200)   -resizes the obj to 300px wide, 200px high
Resize(obj,300,'*')   -resizes to 300 wide and calculates the height proportionally
Resize(obj,'*',200)   -resizes to 200 high and calculates the width proportionally
Resize(obj,300,null)  -resizes protpotionally to make the largest side 300
Resize(obj,null,200)  -resizes proportionally to make the smallest side 200
Resize(obj,300,200,1)  -resizes proportionally to make the object fit in a 300X200px space
Resize(obj,300,'*',0,1)  -calculates and returns result but does not resize object
Resize(obj,300,200,1,1)  -calculates and returns result but does not resize object
Resize(obj,300,'*',0,0,1)  -Shrinks to 300 wide only if obj is larger than 300px wide
*/
_global.Resize= function(obj,maxW,maxH,tofit,returnOnly,disallowScaleUp){
 r=new Object()
 if(tofit){ //constrained fit into max dimensions
  possize=Resize(obj,maxW,'*',0,1,disallowScaleUp) //get possible size
  if(possize.h>maxH){r=Resize(obj,'*',maxH,0,1,disallowScaleUp)}
  else{r=Resize(obj,maxW,'*',0,1,disallowScaleUp)}
 }else if(maxW+maxH>0 && !tofit){//dont constrain
  r.w=maxW
  r.h=maxH
 }else if(maxW && maxH=='*'){ //constrain by width
  proportion=obj._height/obj._width
  r.w=maxW
  r.h=maxW*proportion
 }else if(maxH && maxW=='*'){ //constrain by height
  proportion=obj._width/obj._height
  r.h=maxH
  r.w=maxH*proportion
 }else if(maxW && !maxH){ //constrain by largest side
  if(obj._width>obj._height){
   Resize(obj,maxW,'*',0,returnOnly,disallowScaleUp)
  }else{Resize(obj,'*',maxW,0,returnOnly,disallowScaleUp)}
 }else if(!maxW && maxH){ //constrain by smallest side
  if(obj._width>obj._height){
   Resize(obj,'*',maxH,0,returnOnly,disallowScaleUp)
  }else{Resize(obj,maxH,'*',0,returnOnly,disallowScaleUp)}
 }
 if(disallowScaleUp){
  if(r.w>obj._width){r.w=obj._width}
  if(r.h>obj._height){r.h=obj._height}
 }
 //returnOnly values or perform resize?
 if(!returnOnly){obj._width=r.w;obj._height=r.h;}
 return(r)
}

6 comments:

Anonymous said...

This script sounds exciting, too bad it does not work.

The image resize differently from the original picture size.

cron said...

Thanks for the comment. If you'd left some more details of what you tried that didn't work I could investigate.

Anonymous said...

Thank you, this worked first go and saved me from lots of frustration! :)

Anonymous said...

This script worked fine for me!

Thanks a lot!

Anonymous said...

works perfect! thank you a lot!

Meiqing Xu said...

canada goose
coach factory outlet
coach outlet
ray ban sunglasses
fit flops
canada goose jackets
kate spade outlet
cheap oakleys
birkenstock sandals
birkenstocks
20161228caiyan]
\

Post a Comment

Cron Design Studio: Dublin based web design & software development