Chinaunix首页 | 论坛 | 博客
  • 博客访问: 250428
  • 博文数量: 15
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 88
  • 用 户 组: 普通用户
  • 注册时间: 2017-12-01 10:33
文章分类

全部博文(15)

文章存档

2019年(12)

2018年(2)

2017年(1)

我的朋友

分类: PHP

2019-10-15 09:34:01

下面给大家分享一下PHP给图片加上图片水印和文字水印实例,这也是网站经常用到的功能,把代码加上去,调用就很简单了。


核心代码:
  1. function imageWaterMark($groundImage, $waterPos = 0, $waterImage = "", $waterText = "", $textFont = 5, $textColor = "#FF0000") {
  2.  
  3.     $isWaterImage = FALSE;
  4.  
  5.     $formatMsg = "暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。";
  6.  
  7.     //读取水印文件
  8.  
  9.     if (!empty($waterImage) && file_exists($waterImage)) {
  10.  
  11.         $isWaterImage = TRUE;
  12.  
  13.         $water_info = getimagesize($waterImage);
  14.  
  15.         $water_w = $water_info[0]; //取得水印图片的宽
  16.  
  17.         $water_h = $water_info[1]; //取得水印图片的高
  18.  
  19.         switch ($water_info[2]) {//取得水印图片的格式
  20.             case 1:$water_im = imagecreatefromgif($waterImage);
  21.                 break;
  22.  
  23.             case 2:$water_im = imagecreatefromjpeg($waterImage);
  24.                 break;
  25.  
  26.             case 3:$water_im = imagecreatefrompng($waterImage);
  27.                 break;
  28.  
  29.             default:die($formatMsg);
  30.         }
  31.     }
  32.  
  33.     //读取背景图片
  34.  
  35.     if (!empty($groundImage) && file_exists($groundImage)) {
  36.  
  37.         $ground_info = getimagesize($groundImage);
  38.  
  39.         $ground_w = $ground_info[0]; //取得背景图片的宽
  40.  
  41.         $ground_h = $ground_info[1]; //取得背景图片的高
  42.  
  43.         switch ($ground_info[2]) {//取得背景图片的格式
  44.             case 1:$ground_im = imagecreatefromgif($groundImage);
  45.                 break;
  46.  
  47.             case 2:$ground_im = imagecreatefromjpeg($groundImage);
  48.                 break;
  49.  
  50.             case 3:$ground_im = imagecreatefrompng($groundImage);
  51.                 break;
  52.  
  53.             default:die($formatMsg);
  54.         }
  55.     } else {
  56.  
  57.         die("需要加水印的图片不存在!");
  58.     }
  59.  
  60.     //水印位置
  61.  
  62.     if ($isWaterImage) {//图片水印
  63.         $w = $water_w;
  64.  
  65.         $h = $water_h;
  66.  
  67.         $label = "图片的";
  68.     } else {//文字水印
  69.         $temp = imagettfbbox(ceil($textFont * 5), 0, "./cour.ttf", $waterText); //取得使用 TrueType 字体的文本的范围
  70.  
  71.         $w = $temp[2] - $temp[6];
  72.  
  73.         $h = $temp[3] - $temp[7];
  74.  
  75.         unset($temp);
  76.  
  77.         $label = "文字区域";
  78.     }
  79.  
  80.     if (($ground_w < $w) || ($ground_h < $h)) {
  81.  
  82.         echo "需要加水印的图片的长度或宽度比水印" . $label . "还小,无法生成水印!";
  83.  
  84.         return;
  85.     }
  86.  
  87.     switch ($waterPos) {
  88.  
  89.         case 0://随机
  90.  
  91.             $posX = rand(0, ($ground_w - $w));
  92.  
  93.             $posY = rand(0, ($ground_h - $h));
  94.  
  95.             break;
  96.  
  97.         case 1://1为顶端居左
  98.  
  99.             $posX = 0;
  100.  
  101.             $posY = 0;
  102.  
  103.             break;
  104.  
  105.         case 2://2为顶端居中
  106.  
  107.             $posX = ($ground_w - $w) / 2;
  108.  
  109.             $posY = 0;
  110.  
  111.             break;
  112.  
  113.         case 3://3为顶端居右
  114.  
  115.             $posX = $ground_w - $w;
  116.  
  117.             $posY = 0;
  118.  
  119.             break;
  120.  
  121.         case 4://4为中部居左
  122.  
  123.             $posX = 0;
  124.  
  125.             $posY = ($ground_h - $h) / 2;
  126.  
  127.             break;
  128.  
  129.         case 5://5为中部居中
  130.  
  131.             $posX = ($ground_w - $w) / 2;
  132.  
  133.             $posY = ($ground_h - $h) / 2;
  134.  
  135.             break;
  136.  
  137.         case 6://6为中部居右
  138.  
  139.             $posX = $ground_w - $w;
  140.  
  141.             $posY = ($ground_h - $h) / 2;
  142.  
  143.             break;
  144.  
  145.         case 7://7为底端居左
  146.  
  147.             $posX = 0;
  148.  
  149.             $posY = $ground_h - $h;
  150.  
  151.             break;
  152.  
  153.         case 8://8为底端居中
  154.  
  155.             $posX = ($ground_w - $w) / 2;
  156.  
  157.             $posY = $ground_h - $h;
  158.  
  159.             break;
  160.  
  161.         case 9://9为底端居右
  162.  
  163.             $posX = $ground_w - $w - 10; // -10 是距离右侧10px 可以自己调节
  164.  
  165.             $posY = $ground_h - $h - 10; // -10 是距离底部10px 可以自己调节
  166.  
  167.             break;
  168.  
  169.         default://随机
  170.  
  171.             $posX = rand(0, ($ground_w - $w));
  172.  
  173.             $posY = rand(0, ($ground_h - $h));
  174.  
  175.             break;
  176.     }
  177.  
  178.     //设定图像的混色模式
  179.  
  180.     imagealphablending($ground_im, true);
  181.  
  182.     if ($isWaterImage) {//图片水印
  183.         imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w, $water_h); //拷贝水印到目标文件
  184.     } else {//文字水印
  185.         if (!emptyempty($textColor) && (strlen($textColor) == 7)) {
  186.  
  187.             $R = hexdec(substr($textColor, 1, 2));
  188.  
  189.             $G = hexdec(substr($textColor, 3, 2));
  190.  
  191.             $B = hexdec(substr($textColor, 5));
  192.         } else {
  193.  
  194.             die("水印文字颜色格式不正确!");
  195.         }
  196.  
  197.         imagestring($ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
  198.     }
  199.  
  200.     //生成水印后的图片
  201.  
  202.     @unlink($groundImage);
  203.  
  204.     switch ($ground_info[2]) {//取得背景图片的格式
  205.         case 1:imagegif($ground_im, $groundImage);
  206.             break;
  207.  
  208.         case 2:imagejpeg($ground_im, $groundImage);
  209.             break;
  210.  
  211.         case 3:imagepng($ground_im, $groundImage);
  212.             break;
  213.  
  214.         default:die($errorMsg);
  215.     }
  216.  
  217.     //释放内存
  218.  
  219.     if (isset($water_info))
  220.         unset($water_info);
  221.  
  222.     if (isset($water_im))
  223.         imagedestroy($water_im);
  224.  
  225.     unset($ground_info);
  226.  
  227.     imagedestroy($ground_im);
  228. }

文字水印:
  1. waterText('banner.jpg', 9, '','sucaihuo.com'); //背景图片,$waterPos水印位置,水印图片地址

图片水印:
  1. imageWaterMark('404.jpg', 9, 'logo.gif'); //背景图片,$waterPos水印位置,水印图片地址

注意:$waterImage 和 $waterText 最好不要同时使用,选其中之一即可,优先使用 $waterImage。

本文转自:https://www.sucaihuo.com/php/265.html 转载请注明出处!

阅读(286) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~