好的,我在文件中有两张图片.其中一件是T恤.另一个是徽标.我使用CSS来设置两个图像的样式,以便看起来徽标写在T恤上.我只是在CSS样式表中给徽标的图像提供了更高的z-index.无论如何我是否可以使用GD库生成衬衫和图像组合成一个图像?
谢谢,
长矛
这是可能的.示例代码:
// 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);
Run Code Online (Sandbox Code Playgroud)
如果您不想指定透明色,而是想要使用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);
Run Code Online (Sandbox Code Playgroud)
参考文献:
imagecreatefrompng
imagecolortransparent
imagesx
imagesy
imagecopymerge
imagecopy
从PHP.net到水印图像的教程从PHP.net到水印图像的
教程(使用alpha通道)
| 归档时间: |
|
| 查看次数: |
6050 次 |
| 最近记录: |