Skip to content

GD: imagerotate produces inaccurate results #17982

Closed
@cjhewett

Description

@cjhewett

Description

The following code:

<?php
$gd = imagecreatetruecolor(5000,5000);
imagefill($gd, 0, 0, imagecolorallocate($gd, 255, 255, 255));

$rot = deg2rad(-1);
$a = cos($rot);
$b = sin($rot);

$gdAffine = imageaffine($gd, [$a ,$b, -$b, $a, 0, 0]);

ob_start();
imagejpeg($gdAffine);
$jpg = ob_get_clean();

echo '<img src="data:image/jpg;base64,'.base64_encode($jpg).'">';

$gdRotate = imagerotate($gd, rad2deg(-$rot), 0);

ob_start();
imagejpeg($gdRotate);
$jpg = ob_get_clean();

echo '<img src="data:image/jpg;base64,'.base64_encode($jpg).'">';

Resulted in this output:

  • This rotates a 5000x5000 canvas by 1 degree using both imageaffine and imagerotate
  • This should result in a black triangle at the top of the canvas with a height of 5000*sin(1deg) = 87 pixels
  • Both methods produce a final canvas of 5087x5087 which is the expected size
  • imageaffine produces a triangle of 87px high ✔
  • imagerotate produces a triangle of 73px high ❌ (this implies an actual rotation of ~0.83 degrees)
  • Looking to the right hand side of the top of imagerotate shows substantial clipping, suggesting the image has been scaled up in some way and the edge pixels lost.

The image:
Image

Top left of imageaffine followed by top left of imagerotate:

Image

Top right of imageaffine showing pixels correctly go into edge

Image

Top right of imagerotate showing substantial cropping of pixels along edge.

Image

GD Version: 2.3.3

PHP Version

PHP 8.3.8

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions