带有2个文件的PHP GD图像

时间:2011-03-06 15:05:55

标签: php gd

这是我想要做的事情。建议我对GD2

相当新

我想以这种方式用2张图片制作图像;

背景矩形填充图像1

之后,我想在其上绘制polygon,并填充另一张图片。

我现在所拥有的是背景中的矩形和图像。

我可以绘制多边形,但我无法弄清楚如何用另一个图像填充它。它现在用蓝色填充,我想用另一个图像填充它。

继承我的代码

$values = array(
            40,  50,  // Point 1 (x, y)
            20,  240, // Point 2 (x, y)
            60,  60,  // Point 3 (x, y)
            240, 20,  // Point 4 (x, y)
            50,  40,  // Point 5 (x, y)
            10,  10   // Point 6 (x, y)
        );

$image2 = imagecreatefromjpeg('test2.jpg');
$image = imagecreatefromjpeg('test.jpg');

$bg   = imagecreatefromjpeg('test.jpg');

$fill = imagecolorallocate($image, 0, 0, 255);

// fill the background
imagefilledrectangle($image, 0, 0, 249, 249, $bg);

// draw a polygon
imagefilledpolygon($image, $values, 6, $fill);

// flush image
header('Content-type: image/jpg');
imagepng($image);
imagedestroy($image);

你可以看到imagepng()仅渲染$image如何渲染$ image和$ image2

全部谢谢

2 个答案:

答案 0 :(得分:6)

您需要将第二张图片叠加在第一张图片的顶部。

$file1 = 'test.jpg';
$file2 = 'test2.jpg';

// First image
$image = imagecreatefromjpeg($file1);

// Second image (the overlay)
$overlay = imagecreatefromjpeg($file2);

// We need to know the width and height of the overlay
list($width, $height, $type, $attr) = getimagesize($file2);

// Apply the overlay
imagecopy($image, $overlay, 0, 0, 0, 0, $width, $height);
imagedestroy($overlay);

// Output the results
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);

答案 1 :(得分:0)

我建议你为Image2撕掉imagealphablending,在Image2上绘制一个多边形的倒数,其颜色为alpha值:0。打开imagealphablending。然后你可以将Image2复制到Image1(背景)上。