imagecopyresampled() produces rounding errors
imagecopyresampled() produces rounding errors.
A simple example is trying to resample a fully transparent 560x349 image down to 100x62.
The resulting image will have transparency 2/255, not 0 as it was in the original image.
This is caused by a conversion to int without rounding - alpha/spixels is often something like 126.999999999, that is floored down to int 126, and then 126 is transformed into opacity=2.
The fix would be to change
red /= spixels;
green /= spixels;
blue /= spixels;
alpha /= spixels;
to
red = red/spixels+0.5;
green = green/spixels+0.5;
blue = blue/spixels+0.5;
alpha = alpha/spixels+0.5;
Comments (4)
-
-
To anyone with the same problems;
imagefilter($im,IMG_FILTER_CONTRAST,-1);
before saving restores #FFFFFE to pure white.
-
this issue is fixed in latest php. Also php 5.5 and upcoming gd 2.1 have new functions with better quality and more filtering options. See:
https://github.com/php/php-src/blob/master/ext/gd/libgd/gd_interpolation.c
-
- changed status to resolved
rounding issue fixed in latest php releases.
- Log in to comment
I'm having similar issues with absolutely white images being distorted to #fffffe with imagecopyresampled.
Certain ratios are ok, others are not. The rounding errors probably also increases the jpegsize. From my perspective it's a major and severe bug.
regards, /tj
Bundled gd-version: