Ale*_*pin 10 php gd image-resizing
我试图用PHP缩小PHP中的一些透明图像,每当我这样做时,周围都会添加一个奇怪的黑色边框.
之前

后

码
<?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);
?>
Run Code Online (Sandbox Code Playgroud)
似乎如果我将新维度的代码更改为旧维度(删除- 1),则不会出现黑色边框.因此调整大小会导致问题.
有谁知道什么可能是错的?
编辑:我刚刚意识到它只发生在imagecopyresampled和不发生imagecopyresized.但是,imagecopyresampled提供了更好的视觉效果,如果可能的话我想让它工作.
我认为这里的问题是你的源图像。
您拥有的不是带有 alpha 通道的真彩色 PNG,而是带有透明颜色的索引颜色 PNG。如果您在 Photoshop 中打开图像,这一点会很明显:

该图像已经使用抗锯齿功能创建(这使得黄色文本具有此处看到的白色边框),但是当您重新调整其大小时,子像素计算可能会稍微超出其边界。
我怀疑如果你修复图像,使其带有 alpha 通道的完整 RGB,你就不会遇到这个问题。