I am trying to downsize some transparents images in PHP with GD, and whenever I do, there is a weird black-ish border that is added around it.
Before
After
Code
<?php
$image = imagecreatefromstring(file_get_contents('logo.png'));
$width = imagesx($image);
$height = imagesy($image);
$newWidth = $width - 1;
$newHeight = $height - 1;
$output = imagecreatetruecolor($newWidth, $newHeight);
imagecolortransparent($output, imagecolorallocatealpha($output, 0, 0, 0, 127));
imagealphablending($output, false);
imagesavealpha($output, true);
imagecopyresampled($output, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
header('Content-Type: image/png');
imagepng($output);
?>
It seems that if I change the code for the new dimensions to be the same as the old (removing the - 1
), no black borders appear. So the resize is causing the problem.
Does anyone have an idea what might be wrong?
Edit: I just realized it only happens with imagecopyresampled
and not imagecopyresized
. However, imagecopyresampled
gives a far better visual effect and I'd like to make it work if possible.