0

好的,所以我在一个文件中有两个图像。其中一件是一件T恤。另一个是徽标。我使用 CSS 来设置这两个图像的样式,使其看起来像是写在 T 恤上的徽标。我只是在 CSS 样式表中为徽标图像赋予了更高的 z-index。无论如何,我可以使用 GD 库生成衬衫和图像组合为一个的图像吗?

谢谢,

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

如果您不想指定透明颜色,而是想使用 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

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

于 2011-06-21T16:31:51.237 回答
1

本教程应该让您开始http://www.php.net/manual/en/image.examples.merged-watermark.php并走上正轨

于 2011-06-21T16:30:20.083 回答
1

是的。

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

于 2011-06-21T16:26:35.320 回答