I am getting a strange banding / lines artefact in the transparent part of a resized PNG image using my PHP resizing script and the GD Library. It seems that the alpha channel in the parts of the image which should have an alpha value of 0, is fluctuating between 0 and 2 in a banding pattern.
I have no idea what's going on here, if anyone could shed some light on this?
This is only happening when resizing the image down to this thumbnail size. The large size is resized correctly without any banding.
Image with banding on Staging:
Image without banding on Development:
Enhanced view of the banding artefacts:
(The black pixels represent an alpha value of 0, and the white pixels represent an alpha value of 2).
My two environments are set up as follows:
Development - PHP Version 7.2.2-1+ubuntu14.04.1+deb.sury.org+1 with GD Library:
- GD headers Version 2.2.5
- GD library Version 2.1.1
- FreeType Version 2.5.2
- libJPEG Version 8
- libPNG Version 1.2.50
- libXpm Version 30411
Staging - PHP Version 7.0.33-0ubuntu0.16.04.5 with GD Library:
- GD headers Version 2.1.1
- GD library Version 2.1.1
- FreeType Version 2.6.1
- libJPEG Version 8
- libPNG Version 1.2.54
- libXpm Version 30411
The resizing code is as follows:
public static function resizeImage($filename, $desiredWidth, $desiredHeight, $outputDir, $streamResult = true)
{
$imgSrc = null;
$imgDst = null;
$imginfo = getimagesize($filename);
$type = '';
if($imginfo['mime']=='image/png')
{
$type = ImageResizer::TYPE_PNG;
$imgSrc = imagecreatefrompng($filename);
// imagealphablending($imgSrc, true);
imagesavealpha($imgSrc, true);
}
else if ($imginfo['mime']=='image/jpg' || $imginfo['mime']=='image/jpeg' || $imginfo['mime']=='image/pjpeg')
{
$type = ImageResizer::TYPE_JPEG;
$imgSrc = imagecreatefromjpeg($filename);
}
if (empty($type)) return false;
$oldW = $imginfo[0];
$oldH = $imginfo[1];
$newW = $desiredWidth;
$newH = $desiredHeight;
if ($newW == -1)
{
$aspect = ($oldW / $oldH);
$newW = $desiredHeight*$aspect;
$newH = $desiredHeight;
}
else if ($newH == -1)
{
$aspect = ($oldH / $oldW);
$newW = $desiredWidth;
$newH = $desiredWidth*$aspect;
}
else
{
if ($oldW > $oldH)
{
$newW = $desiredWidth;
$newH = $oldH*($desiredWidth/$oldW);
}
else
{
$newW = $oldW*($desiredHeight/$oldH);
$newH = $desiredHeight;
}
}
$imgDst = imagecreatetruecolor($newW,$newH);
imagealphablending($imgDst, false);
imagesavealpha($imgDst, true);
$colourTransparent = imagecolorallocatealpha($imgDst, 255, 255, 255, 255);
imagecolortransparent($imgDst, $colourTransparent);
// Fill image
imagefill($imgDst, 0, 0, $colourTransparent);
imagecopyresampled($imgDst,$imgSrc,0,0,0,0,$newW,$newH,$oldW,$oldH);
// New save location:
$outputFilename = $outputDir . basename($filename);
$result = false;
if ($type == ImageResizer::TYPE_PNG)
{
$result = imagepng($imgDst,$outputFilename);
}
else if ($type == ImageResizer::TYPE_JPEG)
{
$result = imagejpeg($imgDst,$outputFilename,80);
}
if ($result && $streamResult)
{
header('Content-Type: '.$imginfo['mime']);
if ($type == ImageResizer::TYPE_PNG)
{
imagepng($imgDst);
}
else if ($type == ImageResizer::TYPE_JPEG)
{
imagejpeg($imgDst, null, 80);
}
}
imagedestroy($imgDst);
imagedestroy($imgSrc);
return $result;
}