[英]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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.