Php 图像大小调整png在背景中添加黑色

Php 图像大小调整png在背景中添加黑色,php,image,image-resizing,Php,Image,Image Resizing,我正在使用下面的代码调整图像的大小,但它们的问题是,使用透明背景调整png图像的大小,因为它会用黑色填充背景 以下是图像大小调整的完整代码: function store_uploaded_image($html_element_name, $new_img_width, $new_img_height ,$size ,$target_dir) { $target_file = $target_dir .'image-'.$size.'.png'; $image =

我正在使用下面的代码调整图像的大小,但它们的问题是,使用透明背景调整png图像的大小,因为它会用黑色填充背景

以下是图像大小调整的完整代码:

function store_uploaded_image($html_element_name, $new_img_width, $new_img_height ,$size ,$target_dir) {




    $target_file = $target_dir .'image-'.$size.'.png';


    $image = new SimpleImage();
    $image->load($_FILES[$html_element_name]['tmp_name']);
    $image->resize($new_img_width, $new_img_height);
    $image->save($target_file);
    return $target_file; //return name of saved file in case you want to store it in you database or show confirmation message to user
}


class SimpleImage {

   var $image;
   var $image_type;

   function load($filename) {

      $image_info = getimagesize($filename);
      $this->image_type = $image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {

         $this->image = imagecreatefromjpeg($filename);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {

         $this->image = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {

         $this->image = imagecreatefrompng($filename);
      }
   }
   function save($filename, $image_type=IMAGETYPE_PNG, $compression=75, $permissions=null) {

      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image,$filename,$compression);
      } elseif( $image_type == IMAGETYPE_GIF ) {

         imagegif($this->image,$filename);
      } elseif( $image_type == IMAGETYPE_PNG ) {

         imagepng($this->image,$filename);
      }
      if( $permissions != null) {

         chmod($filename,$permissions);
      }
   }
   function output($image_type=IMAGETYPE_JPEG) {

      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image);
      } elseif( $image_type == IMAGETYPE_GIF ) {

         imagegif($this->image);
      } elseif( $image_type == IMAGETYPE_PNG ) {

         imagepng($this->image);
      }
   }
   function getWidth() {

      return imagesx($this->image);
   }
   function getHeight() {

      return imagesy($this->image);
   }
   function resizeToHeight($height) {

      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      $this->resize($width,$height);
   }

   function resizeToWidth($width) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }

   function scale($scale) {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100;
      $this->resize($width,$height);
   }

   function resize($width,$height) {
      $new_image = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;
   }      

}
编辑:

正如@Marc B所建议的,在
imagecopyresampled
之前使用
imagealpha混合
imagesavealpha
,这项功能就开始工作了

imagealphablending( $targetImage, false );
imagesavealpha( $targetImage, true );

尝试先创建白色图像,然后将[“合并”]()与原始图像合并。您需要启用透明度选项,例如@MarcB working!感谢可能的副本