简体   繁体   English

php:如何在没有黑色或彩色背景的情况下调整png的大小

[英]php : how to resize png without a black or colored background

code updated 代码已更新

here's the code i'm using to upload. 这是我用来上传的代码。 Using a class and then refer to it while uploading. 使用一个类,然后在上载时引用它。 The problem is as we know (but didn't find a solution yet) is the black background. 我们知道的问题是(但尚未找到解决方案)是黑色背景。 any way to keep opacity without changing the background color ? 有什么方法可以保持不透明度而不更改背景颜色?

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);     
}  
}  

and the code to resize after uploading 以及上传后要调整大小的代码

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

} }

1 you need to set the blending mode to false like 1您需要将混合模式设置为false

  <?php
  // Create image
   $im = yourimage;

   // Set alphablending to on
  imagealphablending($im, false); 

and than 然后

 imagesavealpha($im,true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM