插件20:图像的水印

<?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表示不透明)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值