Dimension-aware centre crop to square

Automatically detects image dimensions and applies a centered square crop. Highlights the ability to perform calculations based on runtime image metadata.

INPUT
INPUT — Dimension-aware centre crop to square
OUTPUT
OUTPUT — Dimension-aware centre crop to square
JavaScript
// Dimension-aware centre crop to square
// demo_smart_crop.js
//!OUTPUT: OUTPUT

// demo_smart_crop.js — dimension-aware center crop
//
// Reads the actual image dimensions at runtime and computes
// a centered square crop — runtime dimension access is the point.

const img = Engine.loadImage(INPUT);
const size = Math.min(img.width, img.height);
const offX = Math.floor((img.width  - size) / 2);
const offY = Math.floor((img.height - size) / 2);

img.crop(offX, offY, size, size)
   .saturation(1.15)
   .sharpen(0.25)
   .vignette(0.35)
   .save(OUTPUT);

// © 2026 Michael Lechner · mlc OpticScript · https://mlcgo.eu · Elastic License 2.0