php图片增加中文与图片水印代码

$ico_pic 是你要给图片加水印的水印图片,其它的参数都有详细的说明,如果你正在找这类代码可以下载保存成php文件,再利用后面说的调用方法来调用生成水印图片类代码.

php图片增加中文与图片水印代码如下:

  1. <?php
  2. class smallpic{
  3. private $src_pic;//原图
  4. private $ico_pic = "003.png";//水印图
  5. private $ico_text = "水印";//水印文字
  6. private $small_width;//缩略图宽度
  7. private $small_height;//缩略图高度
  8. private $is_ico_pic = true;//是否加图片水印
  9. private $is_text = true;//是否加文字水印
  10. private $src_x = 20;//水印在原图的x坐标
  11. private $src_y = 20;//水印在原图的y坐标
  12. private $ut = "utf-8";//文字编码
  13. private $font_color = "#990000";//文字水印颜色
  14. private $samll_pic_name = "smallpic";//小图的名称
  15. private $big_pic_name = "bigpic";//大图的名称
  16. function __construct($src_pic,$small_width,$small_height){
  17. $this->checkfile($src_pic);
  18. $this->checkfile($this->ico_pic);
  19. $this->src_pic = $src_pic;
  20. $this->small_width = $small_width;
  21. $this->small_height = $small_height;
  22. }
  23. private function __get($property_name){
  24. return $this->$property_name;
  25. }
  26. private function __set($property_name,$value){
  27. return $this->$property_name = $value;
  28. }
  29. /**
  30. * 取得图片的一些基本信息,类型为array
  31. */
  32. function getimageinfo($image){
  33. return @getimagesize($image);
  34. }
  35. /**
  36. * 把图片加载到php中
  37. * $image 传进来的图片
  38. */
  39. function getimage($image){
  40. $image_info = $this->getimageinfo($image);
  41. switch($image_info[2]){
  42. case 1:
  43. $img = @imagecreatefromgif($image);
  44. break;
  45. case 2:
  46. $img = @imagecreatefromjpeg($image);
  47. break;
  48. case 3:
  49. $img = @imagecreatefrompng($image);
  50. break;
  51. }
  52. return $img;
  53. }
  54. function createimageforsuffix($big_pic,$new_pic){
  55. $image_info = $this->getimageinfo($this->src_pic);
  56. switch($image_info[2]){
  57. case 1:
  58. //输出大图
  59. @imagegif($big_pic,$this->big_pic_name.".gif");
  60. //输出小图
  61. @imagegif($new_pic,$this->samll_pic_name.".gif");
  62. break;
  63. case 2:
  64. //输出大图
  65. @imagejpeg($big_pic,$this->big_pic_name.".jpg");
  66. //输出小图
  67. @imagejpeg($new_pic,$this->samll_pic_name.".jpg");
  68. break;
  69. case 3:
  70. //输出大图
  71. @imagepng($big_pic,$this->big_pic_name.".png");
  72. //输出小图
  73. @imagepng($new_pic,$this->samll_pic_name.".png");
  74. break;
  75. }
  76. }
  77. function checkfile($file){
  78. if(!file_exists($file)){
  79. die("图片:".$file."不存在!");
  80. }
  81. }
  82. function createsmallimage(){
  83. $big_pic = $this->getimage($this->src_pic);
  84. $big_pic_info = $this->getimageinfo($this->src_pic);
  85. $new_pic = $this->getimage($this->ico_pic);
  86. $new_pic_info = $this->getimageinfo($this->ico_pic);
  87. $rgb = $this->convcolor();
  88. //判断是按宽比例缩放还是按高比例缩放
  89. if($big_pic_info[0] > $big_pic_info[1]){
  90. $ratio = $this->small_width/(int)$big_pic_info[0];
  91. $small_pic_width = $this->small_width;
  92. $small_pic_height = (int)($big_pic_info[1]*$ratio);
  93. }else{
  94. $ratio = $this->small_height/(int)$big_pic_info[1];
  95. $small_pic_height = $this->small_height;
  96. $small_pic_width = (int)($big_pic_info[0]*$ratio);
  97. }
  98. //echo $small_pic_width = (int)($big_pic_info[0]*$ratio);
  99. //echo $small_pic_height = (int)($big_pic_info[1]*$ratio);
  100. //是否打图片水印
  101. if ($this->is_ico_pic){
  102. //打图片水印
  103. @imagecopy($big_pic,$new_pic,$this->src_x,$this->src_y,0,0,$new_pic_info[0],$new_pic_info[1]);
  104. }
  105. //是否打文字水印
  106. if ($this->is_text){
  107. //设置文字颜色
  108. $text_color = @imagecolorallocate($big_pic,$rgb[0],$rgb[1],$rgb[2]);
  109. //转换文字编码
  110. $text = @iconv($this->ut,"utf-8",$this->ico_text);
  111. //打文字水印
  112. @imagettftext($big_pic,12,0,$this->src_x,$this->src_y,$text_color,"simkai_0.ttf",$text);
  113. }
  114. //新建一个新图片的画板
  115. $new_pic = @imagecreatetruecolor($small_pic_width,$small_pic_height);
  116. //生成缩略图
  117. @imagecopyresized($new_pic,$big_pic,0,0,0,0,$small_pic_width,$small_pic_height,$big_pic_info[0],$big_pic_info[1]);
  118. //输出图
  119. $this->createimageforsuffix($big_pic,$new_pic);
  120. }
  121. /**
  122. * 类内部的功能函数把#000000转换成255,255,255
  123. */
  124. private function convcolor(){
  125. $rgb = array();
  126. $color = preg_replace("/#/","",$this->font_color);
  127. $c = hexdec($color);
  128. $r = ($c >> 16) & 0xff;
  129. $g = ($c >> 8) & 0xff;
  130. $b = $c & 0xff;
  131. $rgb[0] = $r;
  132. $rgb[1] = $g;
  133. $rgb[2] = $b;
  134. return $rgb;
  135. }
  136. }
  137. //调用方法
  138. $pic = new smallpic("002.jpg",600,300);
  139. $pic->is_text = true;
  140. $pic->is_ico_pic = true;
  141. $pic->ico_pic = "./images/004.png";
  142. $pic->ico_text = "新年快乐!";
  143. //$pic->src_x = 80;
  144. $pic->src_y = 80;
  145. $pic->ut = "utf-8";
  146. $pic->font_color = "#0521f8";
  147. $pic->samll_pic_name = "hslsamll";
  148. $pic->big_pic_name = "hslbig";
  149. $pic->createsmallimage();
  150. //开源代码phpfensi.com
  151. ?>