文件上传 - PHP :如何在没有黑色或彩色背景的情况下调整 PNG 的大小


file upload - php : how to resize png without a black or colored background

代码已更新

这是我用来上传的代码。使用类,然后在上传时引用它。问题是我们知道(但还没有找到解决方案)是黑色背景。有什么方法可以在不改变背景颜色的情况下保持不透明度?

class thumb{   
function load($img){   
$img_info = getimagesize($img);   
$img_type = $img_info[2];   
if($img_type == 1){   
$this->image = imagecreatefromgif($img);     
}  
elseif($img_type == 2){  
$this->image = imagecreatefromjpeg($img);    
}  
elseif($img_type == 3){  
$this->image = imagecreatefrompng($img);     
}  
}  
function get_height(){   
return imagesy($this->image);   
}  
function get_width(){  
return imagesx($this->image);   
}  
function resize($width,$height){   
$img_new = imagecreatetruecolor($width,$height); 
imagealphablending($img_new, false); 
imagesavealpha($img_new,true);
$transparent = imagecolorallocatealpha($img_new, 255, 255, 255, 127);
imagefilledrectangle($img_new, 0, 0, $width, $height, $transparent);      
imagecopyresampled($img_new,$this->image,0,0,0,0,$width,$height,$this->get_width(),$this->get_height());  
}  
function save($img,$img_type = 'imagetype_jpeg'){  
$this->image_type = $img_info[2];   
if($img_type == 'imagetype_gif'){   
imagegif($this->image,$img);     
}  
elseif($img_type == 'imagetype_jpeg'){   
imagejpeg($this->image,$img);     
}  
elseif($img_type == 'imagetype_png'){   
imagepng($this->image,$img);     
}  
}  

以及上传后要调整大小的代码

$mini_img = new thumb;   
$mini_img->load($path.$image);  
$mini_img->resize(200,80);   
$mini_img->save('../logo_thumb/'.$image);

}

1 您需要将混合模式设置为 false,例如

  <?php
  // Create image
   $im = yourimage;
   // Set alphablending to on
  imagealphablending($im, false); 

和比

 imagesavealpha($im,true);