0

さて、ファイルに2つの画像があります。その一つがTシャツです。もう一つはロゴです。ロゴが T シャツに書かれているように見えるように、CSS を使用して 2 つの画像のスタイルを設定しました。CSS スタイル シートで、ロゴのイメージの z-index を高くしただけです。GD ライブラリを使用して、シャツと画像を 1 つに結合した画像を生成する方法はありますか?

ありがとう、

ランス

4

3 に答える 3

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);

透明色を指定したくないが、代わりにアルファ チャネルを使用したい場合は、代わりに を使用する必要が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

PHP.net から画像に透かしを入れるための
チュートリアル PHP.net から画像に透かしを入れるためのチュートリアル (アルファ チャネルを使用)

于 2011-06-21T16:31:51.237 に答える
1

はい。

http://php.net/manual/en/book.image.php

于 2011-06-21T16:26:35.320 に答える
1

このチュートリアルでhttp://www.php.net/manual/en/image.examples.merged-watermark.phpを開始し、正しい軌道に乗ることができます。

于 2011-06-21T16:30:20.083 に答える