这是可能的。示例代码:
// 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);
参考:
图像从png创建 http://www.php.net/manual/en/function.imagecreatefrompng.php
图像颜色透明 http://www.php.net/manual/en/function.imagecolortransparent.php
imagesx http://www.php.net/manual/en/function.imagesx.php
imagesy http://www.php.net/manual/en/function.imagesy.php
图像复制合并 http://www.php.net/manual/en/function.imagecopymerge.php
图像复制 http://www.php.net/manual/en/function.imagecopy.php
PHP.net 图像水印教程 http://www.php.net/manual/en/image.examples.merged-watermark.php
来自 PHP.net 的水印图像教程(使用 Alpha 通道) http://www.php.net/manual/en/image.examples-watermark.php