Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GD: imagerotate produces inaccurate results #17982

Closed
cjhewett opened this issue Mar 6, 2025 · 6 comments
Closed

GD: imagerotate produces inaccurate results #17982

cjhewett opened this issue Mar 6, 2025 · 6 comments

Comments

@cjhewett
Copy link

cjhewett commented Mar 6, 2025

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

@cmb69
Copy link
Member

cmb69 commented Mar 6, 2025

This is almost certainly an upstream issue, at least if you're using a system libgd. What does PHP info report about this?

@cjhewett
Copy link
Author

cjhewett commented Mar 7, 2025

phpinfo():

gd

GD Support enabled
GD headers Version 2.3.3
GD library Version 2.3.3
FreeType Support enabled
FreeType Linkage with freetype
GIF Read Support enabled
GIF Create Support enabled
JPEG Support enabled
PNG Support enabled
WBMP Support enabled
XPM Support enabled
XBM Support enabled
WebP Support enabled
BMP Support enabled
AVIF Support enabled
TGA Read Support enabled

@cmb69
Copy link
Member

cmb69 commented Mar 7, 2025

Thanks for the feedback! So this would indeed be an upstream issue.

Anyhow, while I haven't had a closer look at this particular issue, I presume that it is (at least partially) caused by the fixed integer arithmetic used by gdImageRotate(); see libgd/libgd#951, last two paragraphs of the post.

@cjhewett
Copy link
Author

cjhewett commented Mar 7, 2025

Should I open this as a new issue on https://github.com/libgd/libgd/issues, add this as a comment on the gdFixed issue, or leave it open here?

@cmb69
Copy link
Member

cmb69 commented Mar 7, 2025

In doubt, file a new ticket against libgd. (However, there may not be any feedback for quite a while.)

@cjhewett
Copy link
Author

I have posted this upstream to libgd/libgd#955 so I will close off this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants