Skip to content

Rounding issue when using focal point cropping results in 1px white line #451

Description

@thopra

When using focal point cropping (e.g. test.jpg?w=600&h=250&fit=crop-33-66), depending on the aspect ratio and dimensions of the original image, in some cases the cropped result contains a one pixel white line on either edge.

Cause

This happens, because before the image is cropped, it is scaled down to the given dimensions while keeping aspect ratio. Width and height are rounded (see Manipulators/Size.php) - which as a result, can be less than the width or height that are passed to cropping, causing the crop offset to be wrong and a pixel of the white background color to not be covered by the image.

$image->scale((int) round($resize_width * $zoom), (int) round($resize_height * $zoom));

[$offset_x, $offset_y] = $this->resolveCropOffset($image, $width, $height);
// ^-- $width (or $height) can differ by 1 to width and height of the scaled image, 
// which could be rounded down, resulting in $offset_x being -1

return $image->crop($width, $height, $offset_x, $offset_y, 'transparent');

Example

Here's an example of a 1920x1278 source image:

Image

And here's the result, cropped with the following parameters:

test.jpg?w=600&h=250&fit=crop-0-50

Image

Suggested fix

Simply changing the rounding function for parameters passed to the scaling method mentioned above to ceil() instead of round() fixes the issue, because then the scaled image will have at least the size of the given crop dimensions.

$image->scale((int) ceil($resize_width * $zoom), (int) ceil($resize_height * $zoom));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions