用于合并两个图像的PHP GD库

用于合并两个图像的PHP GD库,php,gdlib,Php,Gdlib,好的,我在一个文件中有两个图像。其中一件是t恤衫。另一个是一个标志。我用CSS设计了这两张图片的样式,这样看起来像是t恤上写的徽标。我只是在CSS样式表中给了一个徽标的图像一个更高的z索引。无论如何,我是否可以使用GD库生成衬衫的图像和合并为一个图像的图像 谢谢 兰斯是的 本教程将帮助您开始学习,并使您走上正确的道路这是可能的。示例代码: // or whatever format you want to create from $shirt = imagecreatefrompng("shir

好的,我在一个文件中有两个图像。其中一件是t恤衫。另一个是一个标志。我用CSS设计了这两张图片的样式,这样看起来像是t恤上写的徽标。我只是在CSS样式表中给了一个徽标的图像一个更高的z索引。无论如何,我是否可以使用GD库生成衬衫的图像和合并为一个图像的图像

谢谢

兰斯

是的


本教程将帮助您开始学习,并使您走上正确的道路

这是可能的。示例代码:

// or whatever format you want to create from
$shirt = imagecreatefrompng("shirt.png"); 

// the logo image
$logo = imagecreatefrompng("logo.png"); 

// You need a transparent color, so it will blend nicely into the shirt.
// In this case, we are selecting the first pixel of the logo image (0,0) and
// using its color to define the transparent color
// If you have a well defined transparent color, like black, you have to
// pass a color created with imagecolorallocate. Example:
// imagecolortransparent($logo, imagecolorallocate($logo, 0, 0, 0));
imagecolortransparent($logo, imagecolorat($logo, 0, 0));

// Copy the logo into the shirt image
$logo_x = imagesx($logo); 
$logo_y = imagesy($logo); 
imagecopymerge($shirt, $logo, 0, 0, 0, 0, $logo_x, $logo_y, 100); 

// $shirt is now the combined image
// $shirt => shirt + logo


//to print the image on browser
header('Content-Type: image/png');
imagepng($shirt);
如果不希望指定透明颜色,而是希望使用alpha通道,则必须使用imagecopy而不是imagecopymerge。像这样:

// Load the stamp and the photo to apply the watermark to
$logo = imagecreatefrompng("logo.png");
$shirt = imagecreatefrompng("shirt.png");

// Get the height/width of the logo image
$logo_x = imagesx($logo); 
$logo_y = imagesy($logo);

// Copy the logo to our shirt
// If you want to position it more accurately, check the imagecopy documentation
imagecopy($shirt, $logo, 0, 0, 0, 0, $logo_x, $logo_y);
参考资料:


我不喜欢迂腐,但你能说得更准确一点吗?谢谢你,伙计。。非常感谢