Transformations
Learn how to perform image transformations such as resizing, rotating, cropping, flipping, shearing, and translating images using BoxLang.
Learn how to resize, rotate, crop, flip, and transform images in BoxLang.
Table of Contents
Resizing Images
Resize to Exact Dimensions
// Resize to exact width and height
ImageNew("photo.jpg")
.resize(800, 600)
.write("resized.jpg");
// With interpolation method
ImageNew("photo.jpg")
.resize(800, 600, "bicubic", 0)
.write("resized-hq.jpg");Interpolation Options:
nearest- Fastest, lowest qualitybilinear- Good balance of speed and quality (default)bicubic- Highest quality, slower
BIF Syntax:
Scaling Images
Scale to Fit (Proportional)
Scales the image to fit within specified dimensions while maintaining aspect ratio:
BIF Syntax:
Resize vs ScaleToFit
Example:
If your image is 1600x900 (16:9 aspect ratio):
resize(800, 600)→ 800x600 (distorted to 4:3)scaleToFit(800, 600)→ 800x450 (maintains 16:9)
Rotating Images
Rotate by Angle
BIF Syntax:
Notes:
Positive angles rotate clockwise
Negative angles rotate counter-clockwise
Canvas size expands to accommodate rotated image
Empty areas filled with background color
Rotate with Interpolation
Cropping Images
Basic Cropping
BIF Syntax:
Center Crop to Square
Creating Thumbnails with Cropping
Flipping & Transposing
Flip Horizontally
Flip Vertically
Transpose Operations
The flip() function supports various transpose operations:
Member Function:
Shearing
Shearing skews the image along an axis:
Shear Horizontally
Shear Vertically
BIF Syntax:
Shear Amount:
Positive values shear in one direction
Negative values shear in opposite direction
Typical range: -1.0 to 1.0
Translation
Translation moves the image within its canvas:
BIF Syntax:
Use Cases:
Adjusting image position
Creating padding/margins
Aligning images for compositing
Drawing Axis Transformations
These transformations affect the coordinate system for subsequent drawing operations, not the image itself.
Translate Drawing Axis
Moves the origin point for drawing operations:
BIF Syntax:
Rotate Drawing Axis
Rotates the coordinate system for drawing:
BIF Syntax:
Shear Drawing Axis
Shears the coordinate system for drawing:
BIF Syntax:
Why Use Drawing Axis Transformations?
Drawing axis transformations are useful for:
Creating rotated text/graphics without rotating the entire image
Perspective effects using shearing
Complex compositions with transformed coordinate systems
Efficient drawing - transform once, draw many times
Complete Transformation Examples
Create Multiple Sizes
Rotate and Crop
Scale, Crop, and Sharpen
Flip and Transform
Performance Tips
Resize before other operations - Smaller images process faster
Choose interpolation wisely:
Use
nearestfor pixel art or when speed is criticalUse
bilinearfor good balance (default)Use
bicubiconly when quality is paramount
Batch transformations - Apply multiple operations before saving
Use scaleToFit() instead of manual ratio calculations
Next Steps
Filters & Effects - Apply blur, sharpen, and color effects
Drawing - Add shapes, lines, and text
Advanced Examples - Real-world transformation pipelines
Last updated
Was this helpful?
