繁体   English   中英

PHP GD库用于合并两个图像

[英]PHP GD library used to merge two images

好的,我在文件中有两个图像。 其中之一是一件T恤。 另一个是徽标。 我使用CSS为这两个图像设置样式,以使徽标看起来像写在T恤上。 我只是在CSS样式表中为徽标图像提供了更高的z-index。 无论如何,我可以使用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);

参考文献:
imagecreatefrompng
图像颜色透明
图片x
形象的
图像复制合并
影印

从PHP.net到水印图像的教程
从PHP.net到水印图像的教程(使用Alpha通道)

本教程将帮助您开始正确的开始http://www.php.net/manual/zh/image.examples.merged-watermark.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM