Forms 上传透明图像会添加黑色背景

Forms 上传透明图像会添加黑色背景,forms,file-upload,transparent,image-resizing,Forms,File Upload,Transparent,Image Resizing,我正试图做一个功能来处理阿凡达上传。 我的问题是,当我上传一个透明的图像时,当我调整它的大小后,它会把所有透明的东西都变成黑色 我尝试过使用imagesavealpha()&imagealphablending()选项,但背景仍然变黑 可能是我瞎了眼,没有看到代码中的问题,但我不知道它为什么会这样做 我可以确认,在上传图像后,图像被移动到图像/化身文件夹后,背景仍然是透明的 这是我当前的代码,我一直在使用.png图像进行测试: function upload_avatar(){ $bas

我正试图做一个功能来处理阿凡达上传。 我的问题是,当我上传一个透明的图像时,当我调整它的大小后,它会把所有透明的东西都变成黑色

我尝试过使用imagesavealpha()&imagealphablending()选项,但背景仍然变黑

可能是我瞎了眼,没有看到代码中的问题,但我不知道它为什么会这样做

我可以确认,在上传图像后,图像被移动到图像/化身文件夹后,背景仍然是透明的

这是我当前的代码,我一直在使用.png图像进行测试:

function upload_avatar(){
    $base_path = SYSTEM_PATH;

    if($_FILES['avatar_img']['tmp_name'] != '') {
        $id = md5($this->user_id());
        $filename = $_FILES['avatar_img']['name'];
        $file_basename = substr($filename, 0, strripos($filename, '.')); // strip extention
        $file_ext = substr($filename, strripos($filename, '.')); // strip name
        $filesize = $_FILES['avatar_img']['size'];
        $newfilename = $id . $file_ext;

        if ($file_ext == ".jpg" || $file_ext == ".JPG" || $file_ext == ".jpeg" || $file_ext == ".png" || $file_ext == ".gif"){
            if($filesize <= 153600){

                move_uploaded_file($_FILES['avatar_img']['tmp_name'], $base_path."/images/avatars/" . $newfilename);

                //resize image form
                list($width, $height) = getimagesize($base_path."/images/avatars/" . $newfilename);
                $scale_height = $height/$width;
                $scale_width = $width/$height;

                //Find height and width of the image
                if($width > $height && $width > 150){
                    $width_new = 150;
                    $height_new = round($width_new*$scale_height);
                }else if($height > $width && $height > 150){
                    $height_new = 150;
                    $width_new = round($height_new*$scale_width);
                }else{
                    $height_new = $height;
                    $width_new = $width;
                }

                switch($file_ext) {
                    case ".jpg" :
                        case ".jpeg":
                            $source = imagecreatefromjpeg($base_path."/images/avatars/" . $newfilename);
                    break;

                    case ".png" :
                        $source = imagecreatefrompng($base_path."/images/avatars/" . $newfilename);
                    break;

                    default:
                        $source = imagecreatefromgif($base_path."/images/avatars/" . $newfilename);
                    break;
                }

                $destination = imagecreatetruecolor($width_new, $height_new);

                imagesavealpha($destination, true);
                imagealphablending($destination, true);

                imagecopyresized($destination, $source, 0, 0, 0, 0, $width_new, $height_new, $width, $height);

                switch($file_ext) {
                    case ".jpg":
                    case ".jpeg":
                        imagejpeg($destination, $base_path."/images/avatars/" . $newfilename, 85);
                    break;

                    case ".png":
                        imagepng($destination, $base_path."/images/avatars/" . $newfilename, 8);
                    break;

                    default:
                        imagegif($destination, $base_path."/images/avatars/" . $newfilename, 85);
                    break;
                }

                return $newfilename;
            }else{
                $this->upload_avatar = '<br />But the avatar was not updated. The avatar\'s size exceeded the 150kb limit. ';
                return '';
            }
        }else{
            $this->upload_avatar = '<br />But the avatar was not updated. The avatar must be one of the following formats: jpg, jpeg, png or gif. ';
            return '';
        }
    }else{
        return '';
    }
}
函数上传_avatar(){
$base\u path=系统路径;
如果($_文件['avatar\u img']['tmp\u name']!=''){
$id=md5($this->user_id());
$filename=$\文件['avatar\u img']['name'];
$file_basename=substr($filename,0,stripos($filename,'.');//条带扩展
$file_ext=substr($filename,stripos($filename,'.');//条带名称
$filesize=$_文件['avatar\u img']['size'];
$newfilename=$id.$file\u ext;
如果($file|u ext==”.jpg“||$file_ext==”.jpg“| |$file_ext==”.jpeg“| |$file_ext==”.png“| |$file_ext==”.gif”){
如果($filesize$height&&$width>150){
$width_new=150;
$height\u new=圆形($width\u new*$scale\u height);
}如果($height>$width&$height>150),则为else{
$height_new=150;
$width\u new=圆形($height\u new*$scale\u width);
}否则{
$height_new=$height;
$width\u new=$width;
}
开关($file\u ext){
案例“.jpg”:
大小写“.jpeg”:
$source=imagecreatefromjpeg($base_path./images/avatars/“$newfilename);
打破
案例“.png”:
$source=imagecreatefrompng($base_path./images/avatars/“$newfilename);
打破
违约:
$source=imagecreatefromformgif($base_path./images/avatars/“$newfilename);
打破
}
$destination=imagecreatetruecolor($width\u new,$height\u new);
imagesavealpha($destination,true);
ImageAlphabling($destination,true);
imagecopyresized($destination,$source,0,0,0,$width\u new,$height\u new,$width,$height);
开关($file\u ext){
案例“.jpg”:
大小写“.jpeg”:
imagejpeg($destination,$base_path./images/avatars/“$newfilename,85);
打破
案例“.png”:
imagepng($destination,$base_path./images/avatars/“$newfilename,8);
打破
违约:
imagegif($destination,$base_path./images/avatars/“$newfilename,85);
打破
}
返回$newfilename;
}否则{
$this->upload_avatar='
但未更新该化身。该化身的大小超过了150kb的限制。'; 返回“”; } }否则{ $this->upload_avatar='
但未更新虚拟形象。虚拟形象必须是以下格式之一:jpg、jpeg、png或gif'; 返回“”; } }否则{ 返回“”; } }
任何帮助都将不胜感激,因为我现在正在发疯

谢谢

可能就是你要找的。第二条注释包含调整PNG和GIF大小的示例,同时保持透明度


p、 我本想将此作为评论添加,但我还没有这样做的权利。

干杯,请看一下:)。我以前使用过这个函数,但我认为也可以使用上面的方法。我显然错了。谢谢,如果没有人能告诉我我的初始代码有什么问题,我会尝试一下。谢谢我将其转换为使用您链接到的函数。我想我会同意的。谢谢