Repository: directus/directus
File: api/src/utils/transformations.ts (TypeScript)

Code before:
import type { File, Transformation, TransformationFormat, TransformationSet } from '@directus/types';
import { clamp } from 'lodash-es';
import type { Region } from 'sharp';

export function resolvePreset({ transformationParams, acceptFormat }: TransformationSet, file: File): Transformation[] {
	const transforms = transformationParams.transforms ? [...transformationParams.transforms] : [];

	if (transformationParams.format || transformationParams.quality) {
		transforms.push([
			'toFormat',
			getFormat(file, transformationParams.format, acceptFormat),
			{
				quality: transformationParams.quality ? Number(transformationParams.quality) : undefined,
			},
		]);
	}

	if ((transformationParams.width || transformationParams.height) && file.width && file.height) {
		let toWidth = transformationParams.width ? Number(transformationParams.width) : undefined;
		let toHeight = transformationParams.height ? Number(transformationParams.height) : undefined;

		/*
		 * When withoutEnlargement is true, clamp target dimensions to original dimensions.
		 * This prevents "bad extract area" errors when using focal points with requested
		 * dimensions larger than the original image. Without this, the extraction region
		 * would be calculated based on the larger target dimensions, but the resize step
		 * can't enlarge the image, resulting in an attempt to extract from non-existent area.
		 * See: https://github.com/directus/directus/issues/22391
		 */
		if (transformationParams.withoutEnlargement) {
			if (toWidth !== undefined) {
				toWidth = Math.min(toWidth, file.width);
			}

			if (toHeight !== undefined) {
				toHeight = Math.min(toHeight, file.height);
			}
		}

		const toFocalPointX = transformationParams.focal_point_x
			? Number(transformationParams.focal_point_x)
			: file.focal_point_x;

		const toFocalPointY = transformationParams.focal_point_y
			? Number(transformationParams.focal_point_y)
			: file.focal_point_y;

		/*
		 * Focal point cropping only works with a fixed size (width x height) when `cover`ing,

Review comment (refactor):
```suggestion
		/*
		 * When withoutEnlargement is true, clamp target dimensions to original dimensions to prevent "bad extract area" errors when using focal points.
		 */
```

Code after:
import type { File, Transformation, TransformationFormat, TransformationSet } from '@directus/types';
import { clamp } from 'lodash-es';
import type { Region } from 'sharp';

export function resolvePreset({ transformationParams, acceptFormat }: TransformationSet, file: File): Transformation[] {
	const transforms = transformationParams.transforms ? [...transformationParams.transforms] : [];

	if (transformationParams.format || transformationParams.quality) {
		transforms.push([
			'toFormat',
			getFormat(file, transformationParams.format, acceptFormat),
			{
				quality: transformationParams.quality ? Number(transformationParams.quality) : undefined,
			},
		]);
	}

	if ((transformationParams.width || transformationParams.height) && file.width && file.height) {
		let toWidth = transformationParams.width ? Number(transformationParams.width) : undefined;
		let toHeight = transformationParams.height ? Number(transformationParams.height) : undefined;

		/*
		 * When withoutEnlargement is true, clamp target dimensions to original dimensions to prevent "bad extract area" errors when using focal points.
		 */
		if (transformationParams.withoutEnlargement) {
			if (toWidth !== undefined) {
				toWidth = Math.min(toWidth, file.width);
			}

			if (toHeight !== undefined) {
				toHeight = Math.min(toHeight, file.height);
			}
		}

		const toFocalPointX = transformationParams.focal_point_x
			? Number(transformationParams.focal_point_x)
			: file.focal_point_x;

		const toFocalPointY = transformationParams.focal_point_y
			? Number(transformationParams.focal_point_y)
			: file.focal_point_y;

		/*
		 * Focal point cropping only works with a fixed size (width x height) when `cover`ing,
		 * since the other modes show the whole image. Sharp by default also simply scales up/down
		 * when only supplied with one dimension, so we **must** check, else we break existing behaviour.
		 * See: https://sharp.pixelplumbing.com/api-resize#resize
		 * Also only crop to focal point when explicitly defined so that users can still `cover` with
		 * other parameters like `position` and `gravity` - Else fall back to regular behaviour