2011-06-21 34 views
0

好的,所以我在一个文件中有两个图像。其中之一是一件T恤。另一个是徽标。我使用CSS来设计两个图像的样式,使其看起来像是在T恤上写上了标志。我只是在CSS样式表中给出了一个更高Z-index的标识图像。无论如何,我可以使用GD库生成衬衫和图像组合为一体的图像?用于合并两个图像的PHP GD库

感谢,

兰斯

回答

7

上这是可能的。示例代码:

// 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
imagecolortransparent
imagesx
imagesy
imagecopymerge
imagecopy

Tutorial from PHP.net to watermark images
Tutorial from PHP.net to watermark images (using an alpha channel)

+0

感谢哥们..谢谢 –