简体   繁体   English

PHP图像上传设置PNG,GIF为黑色

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

I have an image upload script that works great but of course there's a wee problem... for some reason(im sure its a imagecreatetruecolor thing ) when i upload pngs and gifs i just get a black picture... 我有一个图像上传脚本,效果很好,但是当然存在一个小问题...由于某些原因(确保它是imagecreatetruecolor东西),当我上传png和gifs时我得到一张黑色的图片...

here's the function 这是功能

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

any help would be great 任何帮助都会很棒

Cheers 干杯

I use the following code when working with GD: 在使用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);

Please note that imagepng uses 3 parameters. 请注意,imagepng使用3个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM