microsoft/playwright · error · Error
Invalid output dimensions
Error message
Invalid output dimensions
What it means
scaleImageToSize throws 'Invalid output dimensions' when the requested target width or height is <= 0, NaN, or +/- Infinity. The scaler needs a finite positive target to allocate the output buffer and compute sampling weights.
Source
Thrown at packages/isomorphic/imageUtils.ts:54
buffer[to + 3] = 0;
}
}
}
return { data: Buffer.from(buffer), width: size.width, height: size.height };
}
export function scaleImageToSize(image: ImageData, size: { width: number; height: number }): ImageData {
const { data: src, width: w1, height: h1 } = image;
const w2 = Math.max(1, Math.floor(size.width));
const h2 = Math.max(1, Math.floor(size.height));
if (w1 === w2 && h1 === h2)
return image;
if (w1 <= 0 || h1 <= 0)
throw new Error('Invalid input image');
if (size.width <= 0 || size.height <= 0 || !isFinite(size.width) || !isFinite(size.height))
throw new Error('Invalid output dimensions');
const clamp = (v: number, lo: number, hi: number) => (v < lo ? lo : v > hi ? hi : v);
// Catmull–Rom weights
const weights = (t: number, o: Float32Array) => {
const t2 = t * t;
const t3 = t2 * t;
o[0] = -0.5 * t + 1.0 * t2 - 0.5 * t3;
o[1] = 1.0 - 2.5 * t2 + 1.5 * t3;
o[2] = 0.5 * t + 2.0 * t2 - 1.5 * t3;
o[3] = -0.5 * t2 + 0.5 * t3;
};
const srcRowStride = w1 * 4;
const dstRowStride = w2 * 4;
// Precompute X: indices, weights, and byte offsets (idx*4)
const xOff = new Int32Array(w2 * 4); // byte offsets = xIdx*4View on GitHub (pinned to c8fc3bf8d3)
Solutions
- Validate Number.isFinite(size.width) && Number.isFinite(size.height) && size.width > 0 && size.height > 0 before calling.
- Clamp targets to a minimum of 1 with Math.max(1, Math.floor(x)) and ensure the source of the number cannot be NaN.
- Default optional config fields to concrete positive values rather than 0/undefined.
Example fix
// before
const out = scaleImageToSize(img, { width: ratio, height: 0 }); // ratio may be NaN
// after
const w = Number.isFinite(ratio) && ratio > 0 ? Math.floor(ratio) : 1;
const out = scaleImageToSize(img, { width: w, height: 64 }); Defensive patterns
Strategy: validation
Validate before calling
function isValidTargetSize(s: {width:number;height:number}): boolean {
return Number.isFinite(s.width) && Number.isFinite(s.height) && s.width > 0 && s.height > 0;
} Type guard
function isPositiveSize(s: unknown): s is { width: number; height: number } {
return !!s && typeof s === 'object'
&& Number.isFinite((s as any).width) && (s as any).width > 0
&& Number.isFinite((s as any).height) && (s as any).height > 0;
} Try / catch
try { return scaleImageToSize(img, size); }
catch (e) { if (e.message === 'Invalid output dimensions') { size = { width: 1, height: 1 }; return scaleImageToSize(img, size); } throw e; } Prevention
- Clamp computed target dimensions to a minimum of 1 with Math.max(1, Math.floor(x)).
- Guard against NaN/Infinity when deriving sizes from ratios.
- Default optional config fields to concrete positive values.
When it happens
Trigger: Calling scaleImageToSize with a target size where size.width or size.height is 0, negative, NaN, or Infinity. Common when the target size is derived from a computation that can yield NaN (e.g. division by zero) or from an unset/optional field defaulting to 0.
Common situations: Computing a thumbnail size from an aspect ratio that produced NaN/Infinity; reading target dimensions from config that was not populated; clamping logic that collapsed a dimension to 0; passing window innerWidth/Height before layout (0).
Related errors
- Invalid input image
- Error while parsing selector `${selector}` - selector cannot
- Cannot find .trace file
- Invalid glob pattern ${JSON.stringify(glob)}: nested '{' is
- Invalid glob pattern ${JSON.stringify(glob)}: unmatched '}'
AI-assisted analysis of microsoft/playwright@c8fc3bf8d3 (2026-08-12).
Data as JSON: /api/errors/509cc9e804f5ff6a.
Report an issue: GitHub.