PHP、imagecreate、ImageCreateTureColor、imagecopy、imagecopymerge

PHP、imagecreate、ImageCreateTureColor、imagecopy、imagecopymerge,php,gd,Php,Gd,(1) 图像创建 (2) imagecreatetruecolor (3) 影像复制 (4) imagecopymerge 我使用了上面的PHP函数,弄糊涂了 首先我准备了两个png文件 1.png 一个带有透明背景和黑点的QRcode,我用这些代码创建它(只有keypoint,不是全部) 2.png 我制作了一个png,并用MS paint的颜色填充背景 准备好了。那么代码是 <?php //use imagecreate or imagecreatetruecolor $image =

(1) 图像创建

(2) imagecreatetruecolor

(3) 影像复制

(4) imagecopymerge

我使用了上面的PHP函数,弄糊涂了

首先我准备了两个png文件

1.png

一个带有透明背景和黑点的QRcode,我用这些代码创建它(只有keypoint,不是全部)

2.png

我制作了一个png,并用MS paint的颜色填充背景

准备好了。那么代码是

<?php
//use imagecreate or imagecreatetruecolor
$image = imagecreate(50, 50);
//$image = imagecreatetruecolor(50, 50);

//save the alpha channel
imagesavealpha($image, true);

//alphablending set false, then transparent color can cover the canvas
imagealphablending($image, false);

//take a transparent color and fill it
imagefill($image, 0, 0, imagecolorallocatealpha($image, 0, 0, 0, 127));

//draw the ellipse
imagefilledellipse($image, 15, 15, 30, 30, imagecolorallocate($image, 255, 0, 0));

imagepng($image, '3.png');

imagedestroy($image);

//merge image
$im_background = imagecreatefrompng('1.png');
$im_foreground = imagecreatefrompng('3.png');

list($width, $height) = getimagesize('3.png');

//use imagecopy or imagecopymerge
imagecopy($im_background, $im_foreground, (int)35, (int)35, 0, 0, $width, $height);
//imagecopymerge($im_background, $im_foreground, (int)35, (int)35, 0, 0, $width, $height, 100);

imagepng($im_background, 'x-x.png');

imagedestroy($im_background);
imagedestroy($im_foreground);

我建议使用
imagecreate
,而不是
imagecreatetruecolor

使用
imagecreate
时,您定义的第一种颜色将是填充整个画布的背景色。这通常是您的“透明”颜色,但通常这样做更好(阅读:在文件中更有效):

imagecolortransparent($img, imagecolorallocate($img, 255, 0, 255));
这样,你就不用使用alpha通道了,你只是告诉它“我用亮粉色画的任何东西都是透明的”——顺便说一句,这就是GIF图像中透明的工作原理,也是“传统”的方式,从很多年前的老游戏开始

使用
imagecreate
生成的图像,复制工作更容易,因为GD确切地知道什么颜色是“透明的”,因此不应该复制到目标上。当使用
imagecreatetruecolor
时,您将进入复杂而混乱的合成业务


我希望这对你有帮助。GD可能是一个需要掌握的棘手问题,但一旦你掌握了基本知识,你就可以开始了。

感谢分享经验和帮助!所有的capbite.com链接都没有了,这真的让这个问题贬值了。看起来它可能对其他人有用,但不是以这种形式。
imagecolortransparent($img, imagecolorallocate($img, 255, 0, 255));