2011-06-21 66 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

感謝哥們..謝謝 –