【问题标题】:php : how to resize png without a black or colored backgroundphp:如何在没有黑色或彩色背景的情况下调整 png 的大小
【发布时间】:2013-01-08 03:50:14
【问题描述】:

代码更新

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

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

}

【问题讨论】:

    标签: php file-upload background resize png


    【解决方案1】:

    1 你需要将混合模式设置为 false 像

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

     imagesavealpha($im,true);
    

    【讨论】:

    • 代码现在看起来像这样,如果我错了,它不起作用纠正我请函数 resize($width,$height){ $img_new = imagecreatetruecolor($width,$height); imagecopyresampled($img_new,$this->image,0,0,0,0,$width,$height,$this->get_width(),$this->get_height()); imagealphablending($img_new, false);图像保存阿尔法($img_new,真); $this->image = $img_new; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    相关资源
    最近更新 更多