在php中合并多边形上的png



我很困惑,我试图简单地画一个多边形并放在上面,就像一层透明的png。。没有任何成功。一次背景是黑色的,一次一夫多妻制变得不可见。。

这是我的php代码:

header ("Content-type: image/png");
// The png layer
$png = imagecreatefrompng("./300.png"); 
imagealphablending($png, false);
$largeur_source = imagesx($png);
$hauteur_source = imagesy($png);

// The polygon
$polygon_image = imagecreate($largeur_source,$hauteur_source);
$polygon_image_background = imagecolorallocate($polygon_image, 255, 255, 255);
imagecolortransparent($polygon_image, $polygon_image_background); // On rend le fond blanc transparent
$polygon_color = imagecolorallocate($polygon_image,100, 200, 225);
$polygon = array(0,0,
                 982,0,
                 982,48,
                 6,48,
                 6,53,
                 0,47,
                 0,0
                 );
imagefilledpolygon($polygon_image , $polygon , 6 , $polygon_color);
imagecopymerge($polygon_image, $png, 0, 0, 0, 0, $largeur_source, $hauteur_source,100); // black !
//imagecopy($polygon_image, $png, 0, 0, 0, 0, $largeur_source, $hauteur_source); // transparent but no polygon..
imagepng($polygon_image);

我在谷歌上呆了几个小时,测试了很多东西,但。。。

提前感谢

我无法解释为什么您的代码不能按预期工作,但让我提出一种稍微不同的方法:

首先,创建一个真正的彩色图像,而不仅仅是一个"普通"图像:

$polygon_image = imagecreatetruecolor($largeur_source, $hauteur_source);

然后像这样画多边形等:

// ...
$polygon = array(/*...*/);
// Make the whole image transparent
imagefill($polygon_image, 0, 0, $polygon_image_background);
// Draw the polygon
imagefilledpolygon($polygon_image, $polygon, 6, $polygon_color);
// Enable alpha blending
imagealphablending($polygon_image, true);
// Initialize the brush with the png
imagesetbrush($polygon_image, $png);
// Merge the two images by drawing the brush (png) exactly once
// right in the middle of the polygon image:
imageline($polygon_image, $largeur_source / 2, $hauteur_source / 2, $largeur_source / 2, $hauteur_source / 2, IMG_COLOR_BRUSHED);

最新更新