具有透明背景的 PNG = 具有黑色背景的缩略图

PNG with transparent background = thumbnail with black backgroud

你能支持我吗? 我有以下脚本来创建缩略图,效果很好! 但是,当我上传具有透明背景的 PNG 文件时,由于某种原因背景变为黑色。

<?php
// Function for resizing jpg, gif, or png image files
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
    list($w_orig, $h_orig) = getimagesize($target);
    $scale_ratio = $w_orig / $h_orig;
    if (($w / $h) > $scale_ratio) {
           $w = $h * $scale_ratio;
    } else {
           $h = $w / $scale_ratio;
    }
    $img = "";
    $ext = strtolower($ext);
    if ($ext == "gif"){ 
      $img = imagecreatefromgif($target);
    } else if($ext =="png"){ 
      $img = imagecreatefrompng($target);
    } else { 
      $img = imagecreatefromjpeg($target);
    }
    $tci = imagecreatetruecolor($w, $h);
    // imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
    imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
    imagejpeg($tci, $newcopy, 80);
}
?>

如果PNG的背景是透明的,我也需要缩略图透明,你能支持我吗? 任何帮助都会很棒!

提前致谢

尝试替换这个:

imagejpeg($tci, $newcopy, 80);

通过这个:

imagegif($tci, $newcopy, 80);

或取决于图像格式的等效函数:

Imagepng

Imagegif

Imagejpeg

Imagebmp

使用:

$tci = imagecreate($w, $h);

改为:

$tci = imagecreatetruecolor($w, $h);

我的问题现已解决:)