Issue Bug Closed

#109964Floating-point precision error in image crop causes ImageMagick command failure (PHP 8.4)

Stig Nørgaard Færch 06/02/26, 15:47 updated 08/11/26, 08:50

Description

Image processing intermittently fails when cropping is applied. The ImageMagick/GraphicsMagick command receives crop offset values in scientific notation (e.g. 1.1368683772162E-13) which it cannot parse, resulting in an invalid geometry error.

The issue surfaced after upgrading from PHP 8.3 to PHP 8.4 alongside a TYPO3 v12 to v13 upgrade.

Steps to reproduce:

The issue occurs with certain image dimension and crop ratio combinations. It is not consistently reproducible because the outcome depends on whether floating-point arithmetic produces an exact zero or a near-zero residual for a given image.

Root cause:

Area::applyRatioRestriction() in typo3/cms-core/Classes/Imaging/ImageManipulation/Area.php computes offset adjustments like:

$newArea->x += ($this->width - $newArea->width) / 2;
$newArea->y += ($this->height - $newArea->height) / 2;

When the crop area already matches the target ratio, the subtraction should yield 0.0, but floating-point arithmetic can produce near-zero residuals like 1.1368683772162E-13.

In GraphicalFunctions.php line 407, these float values are concatenated directly into the ImageMagick command string:

$command .= ' -crop ' . $cropArea->getWidth() . 'x' . $cropArea->getHeight()
    . '+' . $cropArea->getOffsetLeft() . '+' . $cropArea->getOffsetTop() . '! +repage ';

Why PHP 8.4 triggers this:

The floating-point imprecision in applyRatioRestriction() likely existed in earlier PHP versions as well. However, PHP 8.4 changed how float values are converted to strings, more consistently using scientific notation for very small and very large values. On PHP 8.3, a near-zero value like 1.1368683772162E-13 would typically render as 0 in string concatenation context. PHP 8.4 faithfully renders the scientific notation form, which ImageMagick cannot parse.

Since PHP 8.4 is the primary target runtime for TYPO3 v13, this will affect a growing number of installations as they upgrade.

Suggested fix:

The -crop geometry values are pixel coordinates and should be integers. Casting to int via round() eliminates both the precision error and the scientific notation problem:

$command .= ' -crop ' . (int)round($cropArea->getWidth()) . 'x' . (int)round($cropArea->getHeight())
    . '+' . (int)round($cropArea->getOffsetLeft()) . '+' . (int)round($cropArea->getOffsetTop()) . '! +repage ';

Reviews 3

History 411

  1. Stig Nørgaard Færch commented and changed status Under Review Resolved done 0 % 100 % <span data-processor="relativetime" data-value="2026-06-10T02:00:11+00:00" > 06/10/26, 02:00 </span>

    Applied in changeset commit:2661ddd16ab43966fb96d6044ecfd85d40c72af8.