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 quality

  • bilinear - 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:

  1. Creating rotated text/graphics without rotating the entire image

  2. Perspective effects using shearing

  3. Complex compositions with transformed coordinate systems

  4. 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

  1. Resize before other operations - Smaller images process faster

  2. Choose interpolation wisely:

    • Use nearest for pixel art or when speed is critical

    • Use bilinear for good balance (default)

    • Use bicubic only when quality is paramount

  3. Batch transformations - Apply multiple operations before saving

  4. Use scaleToFit() instead of manual ratio calculations

Next Steps

Last updated

Was this helpful?