最近作頭像上傳用到剪切,只要GIF或者PNG是透明的話,剪切後都會變成黑色的背景圖。測試
解決方案有2種:spa
1.背景圖填充白色的背景。圖片
$white = imagecolorallocate($dstim,255,255,255); imagefilledrectangle($dstim,0,0,$width,$height,$white); imagecolortransparent($dstim,$white);it
2.設置圖片走透明通道。im
$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");img
以上2種方式均測試成功。di