Discuz!官方免费开源建站系统

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索

[发布] DZ2.5的经典图片水印插件

[复制链接]
530761333 发表于 2012-4-17 02:04:38 | 显示全部楼层 |阅读模式
Linux下测试成功.
  1. <?php
  2. Class Gimage{
  3. var $src_image_name = "";                 //输入图片的文件名(必须包含路径名)
  4. var $jpeg_quality = 100;         //jpeg图片质量
  5. var $save_image_file = '';       //输出文件名
  6. var $wm_image_name = "";         //水印图片的文件名(必须包含路径名)
  7. var $wm_image_pos = 4;         //水印图片放置的位置
  8. // 0 = middle
  9. // 1 = top left
  10. // 2 = top right
  11. // 3 = bottom right
  12. // 4 = bottom left
  13. var $wm_image_transition = 100;         //水印图片与原图片的融合度 (1=100)
  14. var $wm_text = "";                 //水印文字(支持中文)
  15. var $wm_text_size = 10;             //水印文字大小
  16. var $wm_text_angle = 0;             //水印文字角度
  17. var $wm_text_pos = 0;               //水印文字放置位置
  18. var $wm_text_font = "";     //水印文字的字体

  19. function create($filename="")
  20. {
  21. if ($filename) $this->src_image_name = trim($filename);

  22. $src_image_type = $this->get_type($this->src_image_name);
  23. $src_image = $this->createImage($src_image_type,$this->src_image_name);
  24. if (!$src_image) return;
  25. $src_image_w=ImageSX($src_image);
  26. $src_image_h=ImageSY($src_image);


  27. if ($this->wm_image_name){
  28.     $this->wm_image_name = trim($this->wm_image_name);
  29.     $wm_image_type = $this->get_type($this->wm_image_name);
  30.     $wm_image = $this->createImage($wm_image_type,$this->wm_image_name);
  31.     $wm_image_w=ImageSX($wm_image);
  32.     $wm_image_h=ImageSY($wm_image);
  33.     $temp_wm_image = $this->getPos($src_image_w,$src_image_h,$this->wm_image_pos,$wm_image);
  34.     $wm_image_x = $temp_wm_image["dest_x"];
  35.     $wm_image_y = $temp_wm_image["dest_y"];
  36.     imageCopyMerge($src_image, $wm_image,$wm_image_x,$wm_image_y,0,0,$wm_image_w,$wm_image_h,$this->wm_image_transition);
  37. }

  38. if ($this->wm_text){
  39.     $temp_wm_text = $this->getPos($src_image_w,$src_image_h,$this->wm_text_pos);
  40.     $wm_text_x = $temp_wm_text["dest_x"];
  41.     $wm_text_y = $temp_wm_text["dest_y"];
  42.     $wm_text_color = imagecolorallocate($src_image, 255,255,255);
  43.     imagettftext($src_image, $this->wm_text_size, $this->wm_angle, $wm_text_x, $wm_text_y, $wm_text_color, ($this->wm_text_font), $this->wm_text);
  44. }

  45. if ($this->save_file)
  46. {
  47. switch ($this->output_type){
  48.   case 'gif':$src_img=ImagePNG($src_image, $this->save_file); break;
  49.   case 'jpeg':$src_img=ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break;
  50.   case 'png':$src_img=ImagePNG($src_image, $this->save_file); break;
  51.   default:$src_img=ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break;
  52. }
  53. }
  54. else
  55. {
  56. if ($src_image_type = "jpg") $src_image_type="jpeg";
  57. header("Content-type: image/{$src_image_type}");
  58. switch ($src_image_type){
  59.   case 'gif':$src_img=ImagePNG($src_image); break;
  60.   case 'jpg':$src_img=ImageJPEG($src_image, "", $this->jpeg_quality);break;
  61.   case 'png':$src_img=ImagePNG($src_image);break;
  62.   default:$src_img=ImageJPEG($src_image, "", $this->jpeg_quality);break;
  63. }
  64. }
  65. imagedestroy($src_image);
  66. }

  67. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  68. /*
  69. createImage   根据文件名和类型创建图片
  70. 内部函数

  71. $type:           图片的类型,包括gif,jpg,png
  72. $img_name: 图片文件名,包括路径名,例如 " ./mouse.jpg"
  73. */
  74. function createImage($type,$img_name){
  75.       if (!$type){
  76.         $type = $this->get_type($img_name);
  77.       }

  78.       switch ($type){
  79.             case 'gif':
  80.                 if (function_exists('imagecreatefromgif'))
  81.                     $tmp_img=@imagecreatefromgif($img_name);
  82.                 break;
  83.             case 'jpg':
  84.                 $tmp_img=imagecreatefromjpeg($img_name);
  85.                 break;
  86.             case 'png':
  87.                 $tmp_img=imageceatefrompng($img_name);
  88.                 break;
  89.             default:
  90.                 $tmp_img=imagecreatefromstring($img_name);
  91.                 break;
  92.       }
  93.       return $tmp_img;
  94. }

  95. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  96. getPos           根据源图像的长、宽,位置代码,水印图片id来生成把水印放置到源图像中的位置
  97. 内部函数

  98. $sourcefile_width:     源图像的宽
  99. $sourcefile_height: 原图像的高
  100. $pos:           位置代码
  101. // 0 = middle
  102. // 1 = top left
  103. // 2 = top right
  104. // 3 = bottom right
  105. // 4 = bottom left
  106. $wm_image:       水印图片ID
  107. */
  108. function getPos($sourcefile_width,$sourcefile_height,$pos,$wm_image=""){
  109.       if ($wm_image){
  110.         $insertfile_width = ImageSx($wm_image);
  111.         $insertfile_height = ImageSy($wm_image);
  112.       }else {
  113.         $insertfile_width = 0;
  114.         $insertfile_height = 0;
  115.       }
  116.       if( $pos == 0 )
  117.       {
  118.                       $dest_x = ( $sourcefile_width / 2 ) - ( $insertfile_width / 2 );
  119.           $dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );
  120.       }
  121.       //top left
  122.       if( $pos == 1 )
  123.       {
  124.                 $dest_x = 0;
  125.           $dest_y = 0;
  126.       }
  127.       //top right
  128.       if( $pos == 2 )
  129.       {
  130.                 $dest_x = $sourcefile_width - $insertfile_width;
  131.           $dest_y = 0;
  132.       }

  133.           //bottom right
  134.     if( $pos == 3 )
  135.     {
  136.                 $dest_x = $sourcefile_width - $insertfile_width;
  137.           $dest_y = $sourcefile_height - $insertfile_height;
  138.     }

  139.           //bottom left
  140.     if( $pos == 4 )
  141.     {
  142.                 $dest_x = 0;
  143.           $dest_y = $sourcefile_height - $insertfile_height;
  144.     }
  145.     return array("dest_x"=>$dest_x,"dest_y"=>$dest_y);
  146. }


  147. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  148. get_type           获得图片的格式,包括jpg,png,gif
  149. 内部函数

  150. $img_name:     图片文件名,可以包括路径名
  151. */
  152. function get_type($img_name)//获取图像文件类型
  153. {
  154. $name_array = explode(".",$img_name);
  155. if (preg_match("/\.(jpg|jpeg|gif|png)$/", $img_name, $matches))
  156. {
  157. $type = $matches[1];
  158. }
  159. else
  160. {
  161. $type = "string";
  162. }
  163. return $type;
  164. }

  165. }

  166. ?>


  167. 2、把需要的字体文件放到"c:\winnt\fonts"目录中,如果没有这个目录就建立一个,否则无法使用字体。至于unix类的服务器则不需要

  168. 3、打开/include/post.php

  169. 在第二行添加


  170. 复制代码
  171. require $discuz_root.'./include/imgmark.php';



  172. 找到

  173. 复制代码
  174. if(!$attach_saved && @is_readable($attach)) {
  175.           @$fp = fopen($attach, 'rb');
  176.           @flock($fp, 2);
  177.           @$attachedfile = fread($fp, $attach_size);
  178.           @fclose($fp);

  179.           @$fp = fopen($target, 'wb');
  180.           @flock($fp, 2);
  181.           if(@fwrite($fp, $attachedfile)) {
  182.                 $attach_saved = true;
  183.           }
  184.           @fclose($fp);
  185.     }


  186. 在下面增加


  187. 复制代码
  188. if(in_array($extension, array('jpg','gif','png'))) {
  189.     echo $discuz_root;
  190.       $tmp_image = new Gimage();
  191.       //$tmp_image->wm_text="Upload By SzBar.com";//要显示在图片中的文字信息
  192.       $tmp_image->wm_image_name= "[img]http://www.verysz.com/image/water.jpg[/img]";//要显示在图片中的水印图片名称,包括路径名
  193.       $tmp_image->wm_text_font = 'arial';//水印文字的字体,也就是在c:/winnt/fonts中的字体文件名
  194.       $tmp_image->save_file=$target;
  195.       $tmp_image->create($target);
  196.   }
复制代码
完成!

说明:
如果要用文字水印请把这么代码前面的//去掉
--------------------------------------------------------------------------------

//$tmp_image->wm_text="Upload By SzBar.com";
然后在这段代码前面加上//
如果系统找不到你所设置的字体,则自动用“宋体”代替。

添加水印的位置是可以修改的,请打开imgmark.php按提示修改就可以了!
我的小情情2 发表于 2012-4-17 02:25:23 | 显示全部楼层
这是真的吗?太好了,谢谢您啊
回复

使用道具 举报

我的小情情2 发表于 2012-4-17 02:25:55 | 显示全部楼层
真的吗?太好了,非常喜欢
回复

使用道具 举报

二号 发表于 2012-4-17 02:26:14 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

我的小情情2 发表于 2012-4-17 02:26:27 | 显示全部楼层
看帖子的都发表一下看法
回复

使用道具 举报

我的小情情2 发表于 2012-4-17 02:27:04 | 显示全部楼层
百度对你友好了
回复

使用道具 举报

牛站长牛 发表于 2012-4-17 02:27:26 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

我的小情情2 发表于 2012-4-17 02:27:37 | 显示全部楼层
顶而不懈,遇到好贴决不能放过
回复

使用道具 举报

我的小情情2 发表于 2012-4-17 02:28:13 | 显示全部楼层
强烈感谢楼主
回复

使用道具 举报

 楼主| 530761333 发表于 2012-4-17 14:24:36 | 显示全部楼层
不客气                     
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|Discuz! 官方站 ( 皖ICP备16010102号 )star

GMT+8, 2024-9-22 23:25 , Processed in 0.106224 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表