我有一个图片上传脚本,效果很好但当然有一个小问题... 出于某种原因(我确定它是一个imagecreatetruecolor的东西),当我上传png和GIF时,我只是得到一张黑色照片......
这是函数
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($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
imagejpeg($tci, $newcopy, 80);
}
function ak_img_thumb($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$src_x = ($w_orig / 2) - ($w / 2);
$src_y = ($h_orig / 2) - ($h / 2);
$ext = strtolower($ext);
$img = "";
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, $h);
imagecopyresampled($tci, $img, 0, 0, $src_x, $src_y, $w, $h, $w, $h);
if ($ext == "gif"){
imagegif($tci, $newcopy);
} else if($ext =="png"){
imagepng($tci, $newcopy);
} else {
imagejpeg($tci, $newcopy, 80);
}
}
任何帮助都会很棒
干杯
答案 0 :(得分:1)
使用GD时我使用以下代码:
$newImage = "the new image name goes here"; //full path
$dst_img = imagecreatetruecolor($new_w,$new_h);
/* fix PNG transparency issues */
imagefill($dst_img, 0, 0, IMG_COLOR_TRANSPARENT);
imagesavealpha($dst_img, true);
imagealphablending($dst_img, true);
imagecopyresampled($dst_img,$img,0,0,0,0,$new_w,$new_h,imagesx($img),imagesy($img));
switch($ext)
{
case 'png' : $img = imagepng($dst_img,"$newImage",9);
break;
case 'jpg' : $img = imagejpeg($dst_img,"$newImage",100);
break;
case 'jpeg' : $img = imagejpeg($dst_img,"$newImage",100);
break;
case 'gif' : $img = imagegif($dst_img,"$newImage");
break;
}
imagedestroy($dst_img);
请注意,imagepng使用3个参数。