免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1674 | 回复: 1
打印 上一主题 下一主题

PHP给图片增加水印得类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-12 22:15 |只看该作者 |倒序浏览

PHP给图片增加水印得类










Php代码
  1. 1.<?php  
  2. 2./*
  3. 3.+--------------------------------------------------------------------------
  4. 4.| 生成加水印的图片类 (支持水印为图片或者文字)
  5. 5.| ============================
  6. 6.| by JackMing (感谢Dash和其他不知道姓名的朋友支持,本类在这些朋友作品的基础上创建)
  7. 7.+--------------------------------------------------------------------------
  8. 8.*/  
  9. 9.  
  10. 10.Class Gimage{  
  11. 11.var $src_image_name = ""; //输入图片的文件名(必须包含路径名)  
  12. 12.var $jpeg_quality = 90; //jpeg图片质量  
  13. 13.var $save_image_file = ''; //输出文件名  
  14. 14.var $wm_image_name = ""; //水印图片的文件名(必须包含路径名)  
  15. 15.var $wm_image_pos = 1; //水印图片放置的位置  
  16. 16.// 0 = middle  
  17. 17.// 1 = top left  
  18. 18.// 2 = top right  
  19. 19.// 3 = bottom right  
  20. 20.// 4 = bottom left  
  21. 21.// 5 = top middle  
  22. 22.// 6 = middle right  
  23. 23.// 7 = bottom middle  
  24. 24.// 8 = middle left  
  25. 25.//other = 3  
  26. 26.var $wm_image_transition = 20; //水印图片与原图片的融合度 (1=100)  
  27. 27.  
  28. 28.var $wm_text = ""; //水印文字(支持中英文以及带有\r\n的跨行文字)  
  29. 29.var $wm_text_size = 20; //水印文字大小  
  30. 30.var $wm_text_angle = 4; //水印文字角度,这个值尽量不要更改  
  31. 31.var $wm_text_pos = 3; //水印文字放置位置  
  32. 32.var $wm_text_font = ""; //水印文字的字体  
  33. 33.var $wm_text_color = "#cccccc"; //水印字体的颜色值  
  34. 34.  
  35. 35.function create($filename="")  
  36. 36.{  
  37. 37.if ($filename) $this->src_image_name = strtolower(trim($filename));  
  38. 38.  
  39. 39.$src_image_type = $this->get_type($this->src_image_name);  
  40. 40.$src_image = $this->createImage($src_image_type,$this->src_image_name);  
  41. 41.if (!$src_image) return;  
  42. 42.$src_image_w=ImageSX($src_image);  
  43. 43.$src_image_h=ImageSY($src_image);  
  44. 44.  
  45. 45.  
  46. 46.if ($this->wm_image_name){  
  47. 47.$this->wm_image_name = strtolower(trim($this->wm_image_name));  
  48. 48.$wm_image_type = $this->get_type($this->wm_image_name);  
  49. 49.$wm_image = $this->createImage($wm_image_type,$this->wm_image_name);  
  50. 50.$wm_image_w=ImageSX($wm_image);  
  51. 51.$wm_image_h=ImageSY($wm_image);  
  52. 52.$temp_wm_image = $this->getPos($src_image_w,$src_image_h,$this->wm_image_pos,$wm_image);  
  53. 53.$wm_image_x = $temp_wm_image["dest_x"];  
  54. 54.$wm_image_y = $temp_wm_image["dest_y"];  
  55. 55.imageCopyMerge($src_image, $wm_image,$wm_image_x,$wm_image_y,0,0,$wm_image_w,$wm_image_h,$this->wm_image_transition);  
  56. 56.}  
  57. 57.  
  58. 58.if ($this->wm_text){  
  59. 59.$this->wm_text = $this->gb2utf8($this->wm_text);  
  60. 60.$temp_wm_text = $this->getPos($src_image_w,$src_image_h,$this->wm_text_pos);  
  61. 61.$wm_text_x = $temp_wm_text["dest_x"];  
  62. 62.$wm_text_y = $temp_wm_text["dest_y"];  
  63. 63.if(preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->wm_text_color, $color))  
  64. 64.{  
  65. 65.$red = hexdec($color[1]);  
  66. 66.$green = hexdec($color[2]);  
  67. 67.$blue = hexdec($color[3]);  
  68. 68.$wm_text_color = imagecolorallocate($src_image, $red,$green,$blue);  
  69. 69.}else{  
  70. 70.$wm_text_color = imagecolorallocate($src_image, 255,255,255);  
  71. 71.}  
  72. 72.  
  73. 73.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);  
  74. 74.}  
  75. 75.  
  76. 76.if ($this->save_file)  
  77. 77.{  
  78. 78.switch ($this->output_type){  
  79. 79.case 'gif':$src_img=ImagePNG($src_image, $this->save_file); break;  
  80. 80.case 'jpeg':$src_img=ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break;  
  81. 81.case 'png':$src_img=ImagePNG($src_image, $this->save_file); break;  
  82. 82.default:$src_img=ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break;  
  83. 83.}  
  84. 84.}  
  85. 85.else  
  86. 86.{  
  87. 87.if ($src_image_type = "jpg") $src_image_type="jpeg";  
  88. 88.header("Content-type: image/{$src_image_type}");  
  89. 89.switch ($src_image_type){  
  90. 90.case 'gif':$src_img=ImagePNG($src_image); break;  
  91. 91.case 'jpg':$src_img=ImageJPEG($src_image, "", $this->jpeg_quality);break;  
  92. 92.case 'png':$src_img=ImagePNG($src_image);break;  
  93. 93.default:$src_img=ImageJPEG($src_image, "", $this->jpeg_quality);break;  
  94. 94.}  
  95. 95.}  
  96. 96.imagedestroy($src_image);  
  97. 97.}  
  98. 98.  
  99. 99./*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  100. 100./*
  101. 101.createImage 根据文件名和类型创建图片
  102. 102.内部函数
  103. 103.
  104. 104.$type: 图片的类型,包括gif,jpg,png
  105. 105.$img_name:图片文件名,包括路径名,例如 " ./mouse.jpg"
  106. 106.*/  
  107. 107.function createImage($type,$img_name){  
  108. 108.if (!$type){  
  109. 109.$type = $this->get_type($img_name);  
  110. 110.}  
  111. 111.  
  112. 112.switch ($type){  
  113. 113.case 'gif':  
  114. 114.if (function_exists('imagecreatefromgif'))  
  115. 115.$tmp_img=@ImageCreateFromGIF($img_name);  
  116. 116.break;  
  117. 117.case 'jpg':  
  118. 118.$tmp_img=ImageCreateFromJPEG($img_name);  
  119. 119.break;  
  120. 120.case 'png':  
  121. 121.$tmp_img=ImageCreateFromPNG($img_name);  
  122. 122.break;  
  123. 123.default:  
  124. 124.$tmp_img=ImageCreateFromString($img_name);  
  125. 125.break;  
  126. 126.}  
  127. 127.return $tmp_img;  
  128. 128.}  
  129. 129.  
  130. 130./*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  131. 131.getPos 根据源图像的长、宽,位置代码,水印图片id来生成把水印放置到源图像中的位置
  132. 132.内部函数
  133. 133.
  134. 134.$sourcefile_width: 源图像的宽
  135. 135.$sourcefile_height: 原图像的高
  136. 136.$pos: 位置代码
  137. 137.// 0 = middle
  138. 138.// 1 = top left
  139. 139.// 2 = top right
  140. 140.// 3 = bottom right
  141. 141.// 4 = bottom left
  142. 142.// 5 = top middle
  143. 143.// 6 = middle right
  144. 144.// 7 = bottom middle
  145. 145.// 8 = middle left
  146. 146.$wm_image: 水印图片ID
  147. 147.*/  
  148. 148.function getPos($sourcefile_width,$sourcefile_height,$pos,$wm_image=""){  
  149. 149.if($wm_image){  
  150. 150.$insertfile_width = ImageSx($wm_image);  
  151. 151.$insertfile_height = ImageSy($wm_image);  
  152. 152.}else {  
  153. 153.$lineCount = explode("\r\n",$this->wm_text);  
  154. 154.$fontSize = imagettfbbox($this->wm_text_size,$this->wm_text_angle,$this->wm_text_font,$this->wm_text);  
  155. 155.$insertfile_width = $fontSize[2] - $fontSize[0];  
  156. 156.$insertfile_height = count($lineCount)*($fontSize[1] - $fontSize[3]);  
  157. 157.}  
  158. 158.  
  159. 159.switch ($pos){  
  160. 160.case 0:  
  161. 161.$dest_x = ( $sourcefile_width / 2 ) - ( $insertfile_width / 2 );  
  162. 162.$dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );  
  163. 163.break;  
  164. 164.  
  165. 165.case 1:  
  166. 166.$dest_x = 0;  
  167. 167.if ($this->wm_text){  
  168. 168.$dest_y = $insertfile_height;  
  169. 169.}else{  
  170. 170.$dest_y = 0;  
  171. 171.}  
  172. 172.break;  
  173. 173.  
  174. 174.case 2:  
  175. 175.$dest_x = $sourcefile_width - $insertfile_width;  
  176. 176.if ($this->wm_text){  
  177. 177.$dest_y = $insertfile_height;  
  178. 178.}else{  
  179. 179.$dest_y = 0;  
  180. 180.}  
  181. 181.break;  
  182. 182.  
  183. 183.case 3:  
  184. 184.$dest_x = $sourcefile_width - $insertfile_width;  
  185. 185.$dest_y = $sourcefile_height - $insertfile_height;  
  186. 186.break;  
  187. 187.  
  188. 188.case 4:  
  189. 189.$dest_x = 0;  
  190. 190.$dest_y = $sourcefile_height - $insertfile_height;  
  191. 191.break;  
  192. 192.  
  193. 193.case 5:  
  194. 194.$dest_x = ( ( $sourcefile_width - $insertfile_width ) / 2 );  
  195. 195.if ($this->wm_text){  
  196. 196.$dest_y = $insertfile_height;  
  197. 197.}else{  
  198. 198.$dest_y = 0;  
  199. 199.}  
  200. 200.break;  
  201. 201.  
  202. 202.case 6:  
  203. 203.$dest_x = $sourcefile_width - $insertfile_width;  
  204. 204.$dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );  
  205. 205.break;  
  206. 206.  
  207. 207.case 7:  
  208. 208.$dest_x = ( ( $sourcefile_width - $insertfile_width ) / 2 );  
  209. 209.$dest_y = $sourcefile_height - $insertfile_height;  
  210. 210.break;  
  211. 211.  
  212. 212.case 8:  
  213. 213.$dest_x = 0;  
  214. 214.$dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );  
  215. 215.break;  
  216. 216.  
  217. 217.default:  
  218. 218.$dest_x = $sourcefile_width - $insertfile_width;  
  219. 219.$dest_y = $sourcefile_height - $insertfile_height;  
  220. 220.break;  
  221. 221.}  
  222. 222.return array("dest_x"=>$dest_x,"dest_y"=>$dest_y);  
  223. 223.}  
  224. 224.  
  225. 225./*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  226. 226.gb2utf8 指定的文字转换为UTF-8格式,包括中英文混合
  227. 227.内部函数
  228. 228.*/  
  229. 229.function gb2utf8($gb)  
  230. 230.{  
  231. 231.if(!trim($gb))  
  232. 232.return $gb;  
  233. 233.$filename="./gb2312.txt";  
  234. 234.$tmp=file($filename);  
  235. 235.$codetable=array();  
  236. 236.while(list($key,$value)=each($tmp))  
  237. 237.$codetable[hexdec(substr($value,0,6))]=substr($value,7,6);  
  238. 238.  
  239. 239.$utf8="";  
  240. 240.while($gb)  
  241. 241.{  
  242. 242.if (ord(substr($gb,0,1))>127)  
  243. 243.{  
  244. 244.$tthis=substr($gb,0,2);  
  245. 245.$gb=substr($gb,2,strlen($gb)-2);  
  246. 246.$utf8.=$this->u2utf8(hexdec($codetable[hexdec(bin2hex($tthis))-0x8080]));  
  247. 247.}  
  248. 248.else  
  249. 249.{  
  250. 250.$tthis=substr($gb,0,1);  
  251. 251.$gb=substr($gb,1,strlen($gb)-1);  
  252. 252.$utf8.=$this->u2utf8($tthis);  
  253. 253.}  
  254. 254.}  
  255. 255.  
  256. 256.return $utf8;  
  257. 257.}  
  258. 258.  
  259. 259.function u2utf8($c)  
  260. 260.{  
  261. 261.$str="";  
  262. 262.if ($c < 0x80)  
  263. 263.{  
  264. 264.$str.=$c;  
  265. 265.}  
  266. 266.else if ($c < 0x800)  
  267. 267.{  
  268. 268.$str.=chr(0xC0 | $c>>6);  
  269. 269.$str.=chr(0x80 | $c & 0x3F);  
  270. 270.}  
  271. 271.else if ($c < 0x10000)  
  272. 272.{  
  273. 273.$str.=chr(0xE0 | $c>>12);  
  274. 274.$str.=chr(0x80 | $c>>6 & 0x3F);  
  275. 275.$str.=chr(0x80 | $c & 0x3F);  
  276. 276.}  
  277. 277.else if ($c < 0x200000)  
  278. 278.{  
  279. 279.$str.=chr(0xF0 | $c>>18);  
  280. 280.$str.=chr(0x80 | $c>>12 & 0x3F);  
  281. 281.$str.=chr(0x80 | $c>>6 & 0x3F);  
  282. 282.$str.=chr(0x80 | $c & 0x3F);  
  283. 283.}  
  284. 284.return $str;  
  285. 285.}  
  286. 286.  
  287. 287./*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  288. 288.get_type 获得图片的格式,包括jpg,png,gif
  289. 289.内部函数
  290. 290.
  291. 291.$img_name: 图片文件名,可以包括路径名
  292. 292.*/  
  293. 293.function get_type($img_name)//获取图像文件类型  
  294. 294.{  
  295. 295.$name_array = explode(".",$img_name);  
  296. 296.if (preg_match("/\.(jpg|jpeg|gif|png)$/", $img_name, $matches))  
  297. 297.{  
  298. 298.$type = strtolower($matches[1]);  
  299. 299.}  
  300. 300.else  
  301. 301.{  
  302. 302.$type = "string";  
  303. 303.}  
  304. 304.return $type;  
  305. 305.}  
  306. 306.  
  307. 307.}  
  308. 308.  
  309. 309.?>  
复制代码
使用方法:
  1. $img = new Gimage();
  2. $img->wm_text = "www.discuz.com";
  3. $img->wm_text_font = "./STXINWEI.TTF";
  4. $img->create("./mouse.jpg");
复制代码
就可以了,其中
mouse.jpg是你要在其上添加水印的图片名称,注意包含路径名
STXINWEI.TTF是字体文件的路径名+文件名
这就是一个简单的测试。如果要调整更复杂的显示效果,只要修改一下类中的属性就可以了,例如把字体放大就可以
$img->wm_text_size = 20;
增加水印图片就可以
$img->wm_image_name="文件名";

论坛徽章:
0
2 [报告]
发表于 2012-03-12 22:16 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP