2015年10月1日 星期四

[php]處理透明背景圖檔

就在要處理圖片之前加入這段就好了
imagesavealpha($source_img, true);
$color = imagecolorallocatealpha($source_img, 0, 0, 0, 127);
imagefill($source_img, 0, 0, $color);

ex:
$imgInfo = getimagesize($source_img);
$width = $imgInfo[0];
$height = $imgInfo[1];

$image_new = imagecreatetruecolor(800, ($height * (800/$width)));

imagesavealpha($image_new , true);
$color = imagecolorallocatealpha($image_new , 0, 0, 0, 127);
imagefill($image_new , 0, 0, $color);

imagecopyresampled($image_new, $source_img, 0, 0, 0, 0, 800, ($height * (800/$width)), $width, $height);

return $image_new;