【php】png 图片压缩 透明底色变黑

需要使用gd库的方法 php需要引入gd扩展支持
/*
 * 图片压缩
----------------------------------------------------------------------
函数:调整图片尺寸或生成缩略图
返回:True/False
参数:
  $Image  需要调整的图片(含路径)
  $Dw=450  调整时最大宽度;缩略图时的绝对宽度
  $Dh=450  调整时最大高度;缩略图时的绝对高度
  $Type=1  1,调整尺寸; 2,生成缩略图
$path='img/';//路径
$phtypes=array(
  'img/gif',
  'img/jpg',
  'img/jpeg',
  'img/bmp',
  'img/pjpeg',
  'img/x-png'
    返回图片名称,不含路径
);
*/
public function Img($Image,$exif=0,$Dw=450,$Dh=450,$Type=1){
  if(!file_exists($Image)){
    return false;
  }
  //如果需要生成缩略图,则将原图拷贝一下重新给$Image赋值
  IF($Type!=1){
      copy($Image,str_replace(".","_x.",$Image));
      $Image=str_replace(".","_x.",$Image);
  }
  //取得文件的类型,根据不同的类型建立不同的对象
  $ImgInfo=getimagesize($Image);
  switch($ImgInfo[2]){
      case 1:
      $Img = @imagecreatefromgif($Image);
      Break;
      case 2:
      $Img = @imagecreatefromjpeg($Image);
      Break;
      case 3:
      $Img = @imagecreatefrompng($Image);
      Break;
  }
  //如果对象没有创建成功,则说明非图片文件
  if(empty($Img)){
      //如果是生成缩略图的时候出错,则需要删掉已经复制的文件
      if($Type!=1){
          unlink($Image);
      }
      return false;
  }
  //如果是执行调整尺寸操作则
  $w=imagesx($Img);
  $h=imagesy($Img);
  $width = $w;
  $height = $h;
  if($Type==1){
      if($width>$Dw){
           $Par=$Dw/$width;
           $width=$Dw;
           $height=$height*$Par;
           if($height>$Dh){
               $Par=$Dh/$height;
               $height=$Dh;
               $width=$width*$Par;
           }
      }elseif($height>$Dh){
           $Par=$Dh/$height;
           $height=$Dh;
           $width=$width*$Par;
      }else{
           $width=$width;
           $height=$height;
      }
      if($ImgInfo[2] == 3)
      {
          imagesavealpha($Img,true);
      }
      $nImg = imagecreatetruecolor($width,$height);   //新建一个真彩色画布
      if($ImgInfo[2] == 3)
      {
          imagealphablending($nImg,false);
          imagesavealpha($nImg,true);
      }

      imagecopyresampled($nImg,$Img,0,0,0,0,$width,$height,$w,$h);//重采样拷贝部分图像并调整大小
      //如果是执行生成缩略图操作则
  }else{
      $nImg = imagecreatetruecolor($Dw,$Dh);
      if($h/$w>$Dh/$Dw){ //高比较大
           $height=$h*$Dw/$w;
           $IntNH=$height-$Dh;
           imagecopyresampled($nImg, $Img, 0, -$IntNH/1.8, 0, 0, $Dw, $height, $w, $h);
      }else{   //宽比较大
           $width=$w*$Dh/$h;
           $IntNW=$width-$Dw;
           imagecopyresampled($nImg, $Img, -$IntNW/1.8, 0, 0, 0, $width, $Dh, $w, $h);
      }
  }
    if(!empty($exif))
    {
        switch ($exif) {
            case 8:
                $image = imagerotate($nImg, 90, 0);
                break;
            case 3:
                $image = imagerotate($nImg, 180, 0);
                break;
            case 6:
                $image = imagerotate($nImg, -90, 0);
                break;
        }
    }
    switch($ImgInfo[2]){
        case 1:
            imagegif($nImg,$Image);
            Break;
        case 2:
            imagejpeg($nImg,$Image);
            Break;
        case 3:
            imagepng($nImg,$Image);
            Break;
    }

  $fileDirArr = explode('/',$Image);
  return end($fileDirArr);
}