PHP 生成透明背景的 PNG 缩略图

最近做博客,使用PHP生成透明背景的PNG缩略图时出了点问题,就是用imagecreatetruecolor()函数创建的图片背景默认是黑色,不能透明.

先看一下代码再说:

<?PHP

$img = imagecreatefrompng($src);
imagesavealpha($img,true);       //这里很重要;
$thumb = imagecreatetruecolor(300,300);
imagealphablending($thumb,false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
imagesavealpha($thumb,true);     //这里很重要,意思是不要丢了$thumb图像的透明色;
if(imagecopyresampled($thumb,$img,0,0,0,0,300,300,300,300)){
    imagepng($thumb,"temp.png");
}

?>

 

代码很简单,就是imagealphablending($thumb,false);与imagesavealpha($thumb,true);很重要.

 

转载: http://chencheng2020.blog.163.com/blog/static/149967620091033724447/

posted @ 2014-12-03 09:38  立己达人  阅读(1216)  评论(0编辑  收藏  举报