<?php // Plug-in 20: Image Watermark
// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link
// This example requires a TTF font file named oldenglish.ttf
// to be in the same directory
PIPHP_ImageWatermark("pic.jpg", "wmark.png", "png", 75,
"Watermark", "oldenglish.ttf", 90, "ffffff", 10);
echo "<img src='wmark.png'>";
function PIPHP_ImageWatermark($fromfile, $tofile, $type,
$quality, $text, $font, $size, $fore, $opacity)
{
// Plug-in 20: Image Watermark
//
// This plug-in takes a supplied image and then adds a
// watermark to it. Several options are available.
// The arguments reruired are:
//
// $fromfile: The filename of the original image
// $tofile: The filename for the watermarked image
// $type: One of jpeg, gif or png
// $quality: Quality of the saved image (0-99):
// Only used if $type is jpeg or png
// 0 = Lowest quality and smallest size
// 99 = Best quality and largest size
// $text: The text to display
// $font: Filename of a TTF font file
// $size: Font size to use
// $fore: The foreground color
// $opacity: Opacity of watermark (0-100)
// 0 = No opacity, maximum transparency (invisible)
// 100 = Maximum opacity, no transparency (fully visible)
$contents = file_get_contents($fromfile);
$image1 = imagecreatefromstring($contents);
$bound = imagettfbbox($size, 0, $font, $text);
$width = $bound[2] + $bound[0] + 6;
$height = abs($bound[1]) + abs($bound[7]) + 5;
$image2 = imagecreatetruecolor($width, $height);
$bgcol = PIPHP_GD_FN1($image2, "fedcba");
$fgcol = PIPHP_GD_FN1($image2, $fore);
imagecolortransparent($image2, $bgcol);
imagefilledrectangle($image2, 0, 0, $width, $height, $bgcol);
imagettftext($image2, $size, 0, 2, abs($bound[5]) + 2,
$fgcol, $font, $text);
imagecopymerge($image1, $image2,
(imagesx($image1) - $width) / 2,
(imagesy($image1) - $height) / 2,
0, 0, $width, $height, $opacity);
switch($type)
{
case "gif": imagegif($image1, $tofile); break;
case "jpeg": imagejpeg($image1, $tofile, $quality); break;
case "png": imagepng($image1, $tofile,
round(9 - $quality * .09)); break;
}
}
function PIPHP_GD_FN1($image, $color)
{
return imagecolorallocate($image,
hexdec(substr($color, 0, 2)),
hexdec(substr($color, 2, 2)),
hexdec(substr($color, 4, 2)));
}
?>
插件说明
插件20需要一个文件名保存最后生成的GIF文件。此外还需要文本内容,字体以及颜色,大小和阴影等参数。具体是:
$fromfile 源图像文件的路径或文件名。
$tofile 保存最后得到的图像文件的路径或文件名。
$type 图像类型,GIF,JPEG或PNG之一。
$quality 最终生成的图像文件的质量(0表示最差,99表示最好)。
$text 文本内容。
$font 需要用到TureType字体的路径或文件名。
$size 字体的大小。
$fore 十六进制表示的前景颜色(如000000)。
$opacity 水印的透明度(0表示透明,100表示不透明)。