PHP, imagecreate, imagecreatetruecolor, imagecopy, imagecopy


PHP, imagecreate, imagecreatetruecolor, imagecopy, imagecopymerge

(1) imagecrecreate

(2) imagecreatetruecolor

(3) imagecopy

(4) imagecopymerge

我使用了上面的PHP函数,却感到困惑。

首先我准备了两个png文件…

1. png http://www.capbite.com/subfolder/lion/test/1.png

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

$image = imagecreate($width, $height);
imagesavealpha($image, true);
$color1 = imagecolorallocatealpha($image, 0, 0, 0, 127);
imageColorallocate($image, 0, 0, 0);
imagefill($image, 0, 0, $color1);
...
imagepng($image, $filename);

2. png http://www.capbite.com/subfolder/lion/test/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);

代码使3.png像这样www.capbite.com/subfolder/lion/test/3.png

现在如果我使用

1.png + imagecate + imagecopy将得到http://www.capbite.com/subfolder/lion/test/4-1.png

1.png + imageccreate + imagecopymerge将得到http://www.capbite.com/subfolder/lion/test/4-2.png

1.png + imagecreatetruecolor + imagecopy将得到http://www.capbite.com/subfolder/lion/test/4-3.png

1.png + imagecreatetruecolor + imagecopymerge将得到http://www.capbite.com/subfolder/lion/test/4-4.png

2.png + imagecate + imagecopy将得到http://www.capbite.com/subfolder/lion/test/5-1.png

2.png + imageccreate + imagecopymerge将得到http://www.capbite.com/subfolder/lion/test/5-2.png

2.png + imagecreatetruecolor + imagecopy将得到http://www.capbite.com/subfolder/lion/test/5-3.png

2.png + imagecreatetruecolor + imagecopymerge将得到http://www.capbite.com/subfolder/lion/test/5-4.png

用不同的方式制作的图片很难描述,所以我粘贴了图片的链接,但我的声誉不足以发布更多的链接或图片…—— Niet编辑;)

我的问题是,只是3.png,它有一个透明的背景,甚至使用不同的创建功能,但当它复制到另一个图像,我创建或MS油漆,结果是我满足。

imagecreate &imagecreatetruecolcolor制作4-1.png和4-3.png ?

imagecopy &Imagecopymerge(即使PCT设置为100)使4-3.png和4-4.png ?

我用MS paint创建的4-3.png和5-3.png的图像有什么不同?

非常感谢!

我建议使用imagecreate,而不是imagecreatetruecolor

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

imagecolortransparent($img, imagecolorallocate($img, 255, 0, 255));
在这种情况下,你并没有使用alpha通道,你只是告诉它"我用亮粉色绘制的任何东西都将是透明的"——这就是透明在GIF图像中的工作原理,顺便说一下,这也是一种"传统"的方式,可以追溯到许多许多年前的老游戏!

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

我希望这对你有所帮助。GD可能很难掌握,但一旦你了解了基本知识,你就可以开始了。