PHP合成透明背景的PNG图片
背景
将png格式的二维码合入到有透明背景的png图片中。
代码如下
$dstImg = '1.png'; //需合入的图片
$srcImg = '2.png'; //背景图片
$dst_x = 177;
$dst_y = 196;
$src_x = 0;
$src_y = 0;
$src_w = imagesx(imagecreatefromstring(file_get_contents($srcImg)));
$src_h = imagesy(imagecreatefromstring(file_get_contents($srcImg)));
$dst_w = imagesx(imagecreatefromstring(file_get_contents($dstImg)));
$dst_h = imagesy(imagecreatefromstring(file_get_contents($dstImg)));
$image=imagecreatetruecolor($src_w, $src_h);//创建图片
$back =imagecolorallocatealpha($image, 255, 255, 255,0);//加透明颜色
imagefill($image,0,0,$back);
//读取图片
$im=imagecreatefrompng($srcImg);
$img2 = imagecreatefrompng($dstImg);
imagecopyresampled($image,$im,0,0,$src_x,$src_y,$src_w,$src_h,$src_w,$src_h);//合拼图片
imagecopyresampled($image,$img2,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$dst_w,$dst_h);//合拼图片
header('Content-type: image/png');
imagepng($image);
//imagepng($image,'1.png');
imagedestroy($image);