[英]PHP GD image with 2 files
这是我正在尝试做的事情。 请告知我是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
谢谢大家
您需要将第二个图像叠加在第一个图像之上。
$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);
我建议您为imagealphablending
撕开imagealphablending
,在imagealphablending
上绘制一个与alpha值为0的颜色相反的多边形。打开imagealphablending
。 然后,您可以将Image2复制到Image1(背景)上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.