Image Resizing and Cropping Utility – AS3

UPDATE: Aha, here we go, this looks much better: Fit Display Object Into Rectange
UPDATE: Or check out CASA lib: http://casalib.org/

A day or two ago my man Kyle asked me if I knew of any good ActionScript image resizing utilities. I said “nay”, did a quick (and unsuccessful) search, and promptly forgot about it. This morning, however, I had to write some PHP to scale and crop images to fit inside fixed rectangles (for desktop downloads) and I remembered that I’d written something to do that very thing many years ago. I found my old code, tweaked it, and rolled it out.

This evening I decided to write an AS3 version – I’ll need it soon enough anyhow. Herewith, my Display Utility containing two static methods: resize(), and resizeAndCrop().

DisplayUtil.resize() will scale a DisplayObject to a target size, with an optional fourth parameter to determine whether the image should fit “inside” (default) or “outside” the target size. For example:

var fitInside:Boolean = false;
DisplayUtil.resize( pic, 400, 300, fitInside );

DisplayUtil.resizeAndCrop() will scale a DisplayObject to fit “outside” a target size and then crop off the excess (by setting a scrollRect on the DisplayObject). The optional fourth and fifth parameters determine how to align the crop (the area to keep – defaults to center, middle). For example:

var ha:String = DisplayUtil.ALIGN_RIGHT;
var va:String = DisplayUtil.ALIGN_BOTTOM;
DisplayUtil.resizeAndCrop( pic, 400, 300, ha, va );

Download:

displayutil.zip

Disclaimer: I’m absolutely sure that someone has already written something to do this, but I couldn’t find anything easily (quickly) enough, and it was fun to write.

UPDATE: Aha, here we go, this looks much better: http://blog.soulwire.co.uk/flash/actionscript-3/fit-a-displayobject-into-a-rectangle/
UPDATE: Or check out CASA lib: http://casalib.org/

Posted in Uncategorized | Leave a comment