PHP GD调整透明图像的大小,给出黑色边框

时间:2011-11-15 18:20:27

标签: php gd image-resizing

我正在尝试用PHP缩小PHP中的一些透明图像,每当我这样做时,周围都会添加一个奇怪的黑色边框。

之前 before

enter image description here

代码

<?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);
?>

似乎如果我将新维度的代码更改为旧版本(删除- 1),则不会出现黑色边框。因此调整大小会导致问题。

有没有人知道可能出现的问题?

编辑:我刚刚意识到它只发生在imagecopyresampled而不是imagecopyresized。但是,imagecopyresampled提供了更好的视觉效果,如果可能的话,我想让它发挥作用。

1 个答案:

答案 0 :(得分:3)

我认为这里的问题是你的源图像。

你所拥有的不是具有alpha通道的真彩色PNG,而是具有透明色的索引色PNG。如果在Photoshop中打开图像,这一点很明显:

Image as seen in Photoshop

此图像是使用抗锯齿功能创建的(这样可以看到黄色文字,此处显示的是白色边框),但是当您重新调整大小时,子像素计算可能会超出其边界范围。< / p>

我怀疑如果您修复图像,使用Alpha通道制作完整的RGB,则不会出现此问题。

相关问题