1

我有一个很好用的图像上传脚本,但当然有一个小问题......由于某种原因(我确定它是一个 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);
}
}

任何帮助都会很棒

干杯

4

1 回答 1

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 个参数。

于 2012-11-27T19:01:04.727 回答