当使用PHP调整大小时,如何保持PNG上的透明背景?

当使用PHP调整大小时,如何保持PNG上的透明背景?,php,colors,resize,png,transparent,Php,Colors,Resize,Png,Transparent,我在使用PHP调整图片大小时遇到了一个问题,尤其是使用具有透明背景的PNG文件,而不是保持透明背景,它会变成黑色背景。我怎样才能解决这个问题 这是用于调整大小的脚本: <?php class resize{ var $image; var $image_type; function load($filename) { $image_info = getimagesize($filename); $this->image

我在使用PHP调整图片大小时遇到了一个问题,尤其是使用具有透明背景的PNG文件,而不是保持透明背景,它会变成黑色背景。我怎样才能解决这个问题

这是用于调整大小的脚本:

<?php         
class resize{

   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_JPEG, $compression=85, $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;
   }      

}
?>
编辑:

我已将我的调整大小功能更改为:

function resize($width,$height) {

      $new_image = imagecreatetruecolor($width, $height);
      $transparent=imagefill($new_image, 0, 0, imagecolorallocatealpha($new_image, 255, 255, 255, 127));
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;
   }  
但现在它保持背景白色,而不是透明

编辑2:

已解决,此调整大小功能已损坏,此功能工作正常:

多亏了
为了链接它:D

我找到了另一篇带有相同问题的帖子,根据该帖子,以下代码可用于保持透明性

imagealphablending( $targetImage, false );
imagesavealpha( $targetImage, true );
参考:


如果对你有帮助,请投赞成票。:)

imagecopyresampled()代码行之后,需要使用
imagepng($new\u image,$file\u name)
。 将$file\u name替换为要保存图像的名称

php
imagepng()
-用于从创建的图像中输出PNG图像

此函数有效

function resize($width,$height) {
    $new_image = imagecreatetruecolor($width, $height);
    $transparent=imagefill($new_image, 0, 0, imagecolorallocatealpha($new_image, 255, 255, 255, 127));
    imagealphablending($new_image, false);
    imagesavealpha($new_image, true);
    imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
    $this->image = $new_image;
}  

嘿,谢谢你的主人;但是我觉得我做错了什么,或者把它放错行了,你能告诉我应该把它放在哪里吗?请把它放在imagecolorallocatealpha()函数前面。我的天啊,我很生气,我什么都试过了,什么都不起作用:@@user1773801将重新调整大小的代码粘贴到这里,我将告诉您如何进行调整。这是调整大小的函数:
函数resize($width,$height){$new\u image=ImageCreateTureColor($width,$height);imagecopyresampled($new\u image,$this->image,0,0,$width,$height,$this->getWidth(),$this->getHeight())$this->image=$new_image;}
顺便说一句,它也在上面,它是脚本上的最后一个函数。这个函数有效。。。。函数resize($width,$height){$new_image=imagecreatetruecolor($width,$height);$transparent=imagefill($new_image,0,0,imagecoloralallocatealpha($new_image,255,255,127));imagealphablending($new_image,false);imagesavealpha($new_image,true);imagecopyresampled($new_image,$this->image,0,0,0,$width,$height,$this->getHeight(),$this->getHeight());$this->image=$new_image;}
function resize($width,$height) {
    $new_image = imagecreatetruecolor($width, $height);
    $transparent=imagefill($new_image, 0, 0, imagecolorallocatealpha($new_image, 255, 255, 255, 127));
    imagealphablending($new_image, false);
    imagesavealpha($new_image, true);
    imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
    $this->image = $new_image;
}