Posted By

kaptnkill on 02/27/10


Tagged

width resize image height as3 ratio


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

alvincrespo
davidino1978
svaye
tspitzr


AS3 Resize with ratio


 / Published in: ActionScript 3
 

Usage:

var newDimensions:Object = resize(800,600,320,200);

This can be adapted to pretty much any language...

  1. /**
  2. * resize
  3. * Take input diemnsion and return the resulting dimensions using a ratio
  4. * @param _w initial width
  5. * @param _h initial height
  6. * @param _targetW targeted width
  7. * @param _targetH targeted height
  8. * @param _ratio set the ratio manually instead of automatically detecting it (0)
  9. * @return An object containing both resulting dimensions :object.width,object.height
  10. */
  11. function resize(_w:Number,_h:Number,_targetW:Number,_targetH:Number,_ratio:Number = 0):Object {
  12. var ratio:Number = (_ratio ==0) ? _w / _h : _ratio;
  13. var rW:Number = _targetW;
  14. var rH:Number = _targetH;
  15. if (ratio >= 1) {
  16. rH = _targetW / ratio;
  17. if (rH > _targetH) {
  18. rH = _targetH;
  19. rW = rH * ratio;
  20. }
  21. }else {
  22. rW = _targetH * ratio;
  23. if (rW > _targetW) {
  24. rW = _targetW;
  25. rH = rW / ratio;
  26. }
  27. }
  28. var newDimensions:Object = {width:rW,height:rH };
  29. return newDimensions;
  30. }

Report this snippet  

You need to login to post a comment.