用背景颜色填充透明图像

时间:2013-03-01 21:48:41

标签: php image function

我的网站上有这样的http://drksde.tk/images/F3-big.png图片,用于用户的头像。并且每个用户都可以更改背景的颜色,因为图像具有透明背景。 我做完了!!非常感谢你的帮助!!

以下是代码:

<?php
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){ 
    $cut = imagecreatetruecolor($src_w, $src_h); 
    imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h); 
    imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h); 
    imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct); 
}

$background = imagecreatetruecolor(200, 200);

$color = imagecolorallocate($background, 255, 150, 0);
imagefill($background, 0, 0, $color);

$avatar = imagecreatefrompng('F3-big.png');

imagecopymerge_alpha($background, $avatar, 0, 0, 0, 0, 200, 200, 100);

header('Content-Type: image/png');
imagepng($background);

imagedestroy($background);
imagedestroy($avatar);

&GT;

最后的头像:http://drksde.tk/images/avatar2.png

0 个答案:

没有答案