Hi, I am using the following function which works fine however when uploading PNG's that are transparent it does not keep the transparancy and makes it black. What do i need to change on the below to keep the transparency? function uploadimage($value, $alwidth) { $file_type = $value['type']; $file_name = $value['name']; $file_size = $value['size']; $file_tmp = $value['tmp_name']; //check file extension $ext = strrchr($file_name,'.'); $ext = strtolower($ext); if (($extlimit == "yes") && (!in_array($ext,$limitedext))) { echo "Wrong file extension. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>"; exit(); } //get the file extension. $getExt = explode ('.', $file_name); $file_ext = $getExt[count($getExt)-1]; //create a random file name $rand_name = md5(time()); $rand_name= rand(0,999999999); //get the new width variable. list($ewidth, $eheight) = getimagesize($file_tmp); if ($ewidth < 250){ $ThumbWidth = $ewidth; }else{ $ThumbWidth = 250; } if ($alwidth == ''){ if ($ewidth < 600){ $ImgWidth = ($ewidth + 40); }else{ $ImgWidth = 640; } }else{ $ImgWidth = $alwidth; } if ($ewidth < 420){ $medWidth = $ewidth; }else{ $medWidth = 420; } //keep image type if($file_size){ if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){ $new_img = imagecreatefromjpeg($file_tmp); }elseif($file_type == "image/x-png" || $file_type == "image/png"){ $new_img = imagecreatefrompng($file_tmp); }elseif($file_type == "image/gif"){ $new_img = imagecreatefromgif($file_tmp); } //list width and height and keep height ratio. list($width, $height) = getimagesize($file_tmp); $bigger = ($width >= $height) ? $width : $height; $imgratio = $bigger / $ThumbWidth; $newwidth = intval($width / $imgratio); $newheight = intval($height / $imgratio); $imgratio = $bigger / $ImgWidth; $newwidth2 = intval($width / $imgratio); $newheight2 = intval($height / $imgratio); $imgratio = $bigger / $medWidth; $newwidth3 = intval($width / $imgratio); $newheight3 = intval($height / $imgratio); $resized_img = imagecreatetruecolor($newwidth, $newheight); imagecopyresampled($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); $resized_img2 = imagecreatetruecolor($newwidth2, $newheight2); imagecopyresampled($resized_img2, $new_img, 0, 0, 0, 0, $newwidth2, $newheight2, $width, $height); $resized_img3 = imagecreatetruecolor($newwidth3, $newheight3); imagecopyresampled($resized_img3, $new_img, 0, 0, 0, 0, $newwidth3, $newheight3, $width, $height); imagejpeg($resized_img, "imgtemp/thumbs/".$rand_name.".".$file_ext,100); imagejpeg($resized_img2, "imgtemp/big/".$rand_name.".".$file_ext,100); imagejpeg($resized_img3, "imgtemp/med/".$rand_name.".".$file_ext,100); //save images ftpupload($rand_name.".".$file_ext, "imgtemp/thumbs/".$rand_name.".".$file_ext, "/httpdocs/uploads/images/thumbs/"); ftpupload($rand_name.".".$file_ext, "imgtemp/big/".$rand_name.".".$file_ext, "/httpdocs/uploads/images/big/"); ftpupload($rand_name.".".$file_ext, "imgtemp/med/".$rand_name.".".$file_ext, "/httpdocs/uploads/images/med/"); unlink("imgtemp/big/".$rand_name.".".$file_ext); unlink("imgtemp/thumbs/".$rand_name.".".$file_ext); unlink("imgtemp/med/".$rand_name.".".$file_ext); imagedestroy($resized_img); imagedestroy($resized_img2); imagedestroy($resized_img3); imagedestroy($new_img); } $value = $rand_name.".".$file_ext; return $value; } PHP: Thanks in advance. Adam
Hello, Google 'keep png transparency when uploading' and the first result from Stack Overflow looks promising.
or you have to allocate the 'color' (normal black) to transparant. http://php.net/manual/en/function.imagecolortransparent.php