PNG 图像大小调整无法正常工作

PNG Image resizing doesn't work properly

我正在使用 PHP 测试图片上传功能,但我无法使图片完全透明,只有图片的开始部分得到处理,图片的其余部分保持黑色,这是调整大小之前图片:

http://s13.postimg.org/3jswfzmx3/xnationals_png_pagespeed_ic_k_Mnf2qx2k.png

当我使用调整大小功能时,我得到了这个:

http://s10.postimg.org/4el00d5o9/389056751644.png

这是我得到的代码:

$img = imagecreatefrompng($target);
$tci = imagecreatetruecolor($width, $height);
etruecolor(200, 200);
imagecopyresampled($tci, $img, 0, 0, 0, 0, 200, 200, $w_orig, $h_orig);
imagealphablending($tci, true);
imagesavealpha($tci, true);
imagefill($tci,0,0,0x7fff0000);
imagepng($tci, $newcopy, 9);
imagedestroy($tci);

如果您正在调整 png 大小并且图像透明,则情况会有所不同。你也没有 imagecolorallocatealpha

下面是这个问题的基本解决方法,将它保存在一个函数中以便它可以重复使用,或者在你这样做之前先尝试一下:

function resizeImg($im, $dst_width, $dst_height) {
    $width = imagesx($im);
    $height = imagesy($im);

    $newImg = imagecreatetruecolor($dst_width, $dst_height);

    imagealphablending($newImg, false);
    imagesavealpha($newImg, true);
    $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
    imagefilledrectangle($newImg, 0, 0, $width, $height, $transparent);
    imagecopyresampled($newImg, $im, 0, 0, 0, 0, $dst_width, $dst_height, $width, $height);

    return $newImg;
}