最近做头像上传用到剪切,只要GIF或者PNG是透明的话,剪切后都会变成黑色的背景图。
?
解决方案有2种:
?
1.背景图填充白色的背景。
?
$white = imagecolorallocate($dstim,255,255,255);imagefilledrectangle($dstim,0,0,$width,$height,$white);imagecolortransparent($dstim,$white);
로그인 후 복사
?
2.设置图片走透明通道。
?
$img = imagecreatefrompng($src);imagesavealpha($img,true);//这里很重要;$thumb = imagecreatetruecolor(300,300);imagealphablending($thumb,false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;imagesavealpha($thumb,true);//这里很重要,意思是不要丢了$thumb图像的透明色;imagecopyresampled($thumb,$img,0,0,0,0,300,300,300,300);imagepng($thumb,"temp.png");
로그인 후 복사
?
以上2种方式均测试成功。