Skip to content

[4.x/5.x]: Rounding problem with image transforms in fit mode #16622

@JeroenJRP

Description

@JeroenJRP

What happened?

Description

We ran into an issue where our client uploaded an image with the dimensions 1375x793px and we ran that through a transform with mode 'fit' and width 840, height is left empty (auto).

The resulting image is 839x484 pixels. This is rendered in a container that expects the image to be 840 wide which result in a blurry image.

The culprit seems to be this line in image/Raster.php:

$factor = max($this->getWidth() / $targetWidth, $this->getHeight() / $targetHeight);

https://github.com/craftcms/cms/blob/c4a01cba172b85205a02d986223217a9f6d808b8/src/image/Raster.php#L260C13-L260C97

In this case the height factor is slightly higher and takes precedence over the width factor even though the transform rules specify that only the width is important and the height of the resulting image doesn't matter.

Steps to reproduce

  1. Upload an 1375x793px image to Assets, i.e.: https://dummyimage.com/1375x793
  2. Set up a transform with mode fit, width 840 and height empty
  3. Run the transform on the asset

Expected behavior

Get a transformed image with a width of 840px and a height around 484px

Actual behavior

The transformed image has a width of 839px and a height of 484px

Craft CMS version

4.14.3

PHP version

8.2.26

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions