array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'image.examples-watermark.php', 1 => '使用 Alpha 通道为图像加水印', ), 'up' => array ( 0 => 'image.examples.php', 1 => '示例', ), 'prev' => array ( 0 => 'image.examples-png.php', 1 => '使用 PHP 创建 PNG 图像', ), 'next' => array ( 0 => 'image.examples.merged-watermark.php', 1 => '使用 imagecopymerge 函数创建半透明水印', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'zh', 'path' => 'reference/image/examples.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
示例 #1 使用 Alpha 通道为图像加水印
<?php
// 加载水印以及要加水印的图像
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpeg');
// 设置水印图像的外边距,并且获取水印图像的尺寸
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// 利用图像的宽度和水印的外边距计算位置,并且将水印复制到图像上
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
// 输出图像并释放内存
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>