2011-06-21 9 views
0

これで、ファイルに2つのイメージがあります。そのうちの1つはTシャツです。もう1つはロゴです。私はロゴがTシャツに書かれているように、2つのイメージをスタイルするためにCSSを使用しました。私は単にロゴのイメージにCSSスタイルシートのより高いzインデックスを与えました。とにかく、私はシャツと画像の両方をGDライブラリを使用して合成した画像を生成することができますか?2つのイメージをマージするために使用される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); 

あなたは透明色を指定したいのですが、代わりにアルファチャンネルを使用しない場合、あなたはimagecopymergeの代わりにimagecopyを使用する必要があります。このように:

// 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

ありがとうございました..ありがとう –