簡體   English   中英

PHP圖像上傳設置PNG,GIF為黑色

[英]php image upload setting png, gifs to black

我有一個圖像上傳腳本,效果很好,但是當然存在一個小問題...由於某些原因(確保它是imagecreatetruecolor東西),當我上傳png和gifs時我得到一張黑色的圖片...

這是功能

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);
}
}

任何幫助都會很棒

干杯

在使用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個參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM