Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中合并多边形上的png_Php_Image - Fatal编程技术网

在php中合并多边形上的png

在php中合并多边形上的png,php,image,Php,Image,我很困惑,我试着简单地画一个多边形并放在上面,就像一个带有透明胶片的png图层。。没有任何成功。有一次背景是黑色的,有一次背景是不可见的 以下是我的php代码: header ("Content-type: image/png"); // The png layer $png = imagecreatefrompng("./300.png"); imagealphablending($png, false); $largeur_source = imagesx($png); $hauteur

我很困惑,我试着简单地画一个多边形并放在上面,就像一个带有透明胶片的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);

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

首先,创建真彩色图像,而不仅仅是普通图像:

$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);

我在这里有点困惑