【发布时间】:2016-12-29 10:03:53
【问题描述】:
你能支持我吗? 我有以下脚本来创建缩略图,工作正常! 但是,当我上传具有透明背景的 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的背景是透明的,我也需要缩略图透明的,你能支持我吗? 任何帮助都会很棒!
提前致谢
【问题讨论】:
-
我不完全确定您的代码应该如何工作,但看起来您的缩略图都是 JPEG? JPEG 根本不支持透明度,因此源图像中透明的区域必须填充一些颜色。
标签: php png transparency