php图片水印类,php添加中文水印代码

WBOY
Libérer: 2016-07-25 08:51:48
original
869 Les gens l'ont consulté
  1. Header("Content-type: image/png"); /*通知浏览器,要输出图像*/
  2. $im = imagecreate(400 , 300); /*定义图像的大小*/
  3. $gray = ImageColorAllocate($im , 235 , 235 , 235);
  4. $pink = ImageColorAllocate($im, 255 , 128 , 255);
  5. $fontfile = "simkai.ttf";
  6. /* $fontfile 字体的路径,视操作系统而定,可以是 simhei.ttf(黑体) , SIMKAI.TTF(楷体) , SIMFANG.TTF(仿宋) ,SIMSUN.TTC(宋体&新宋体) 等 GD 支持的中文字体*/
  7. $str = iconv('GB2312','UTF-8','中文水印'); /*将 gb2312 的字符集转换成 UTF-8 的字符*/
  8. ImageTTFText($im, 30, 0, 100, 200, $pink , $fontfile , $str);
  9. /* 加入中文水印 */
  10. Imagepng($im);
  11. ImageDestroy($im);
  12. ?>
复制代码

例2,php图片水印代码。

  1. // **************************************** //
  2. // 功能:给图片添加文字
  3. // 参数: $img 图片文件名
  4. // $new_img 另存图片文件名,如果为空表示不另存图片
  5. // $text 字符串内容
  6. // text_size 字符串大小
  7. // text_angle 字型串输出角度
  8. // text_x 字符串输出 x 坐标
  9. // text_y 字符串输出 y 坐标
  10. // $text_font 字型文件名
  11. // $r,$g,$b 字符串颜色RGB值
  12. // **************************************** //
  13. function img_text($img, $new_img, $text, $text_size, $text_angle, $text_x, $text_y, $text_font, $r, $g, $b){
  14. $text=iconv("gb2312","UTF-8",$text);
  15. Header("Content-type: image/gif");
  16. $im = @imagecreatefromstring(file_get_contents($img)) or die ("打开图片失败!");
  17. $color = ImageColorAllocate($im, $r,$g,$b);
  18. //ImageTTFText(int im, int size, int angle, int x, int y, int col, string fontfile, string text):
  19. //本函数将 TTF (TrueType Fonts) 字型文字写入图片。
  20. //参数: size 为字形的尺寸;
  21. // angle 为字型的角度,顺时针计算,0 度为水平(由左到右),90 度则为由下到上的文字;
  22. // x,y 二参数为文字的坐标值 (原点为左上角);
  23. // col 为字的颜色;
  24. // fontfile 为字型文件名称;
  25. // text 是字符串内容。
  26. ImageTTFText($im, $text_size, $text_angle, $text_x, $text_y, $color, $text_font, $text);
  27. if ($new_img==""):
  28. ImageGif($im); // 不保存图片,只显示
  29. else:
  30. ImageGif($im,$new_img); // 保存图片,但不显示
  31. endif;
  32. ImageDestroy($im); //结束图形,释放内存空间
  33. }
  34. ?>
复制代码

例3,php图片水印,支持php文字水印效果。

  1. /*
  2. * 功能:PHP图片水印 (水印支持图片或文字)
  3. * 参数:
  4. * $groundImage 背景图片,即需要加水印的图片,暂只支持GIF,JPG,PNG格式;
  5. * $waterPos 水印位置,有10种状态,0为随机位置;
  6. * 1为顶端居左,2为顶端居中,3为顶端居右;
  7. * 4为中部居左,5为中部居中,6为中部居右;
  8. * 7为底端居左,8为底端居中,9为底端居右;
  9. * $waterImage 图片水印,即作为水印的图片,暂只支持GIF,JPG,PNG格式;
  10. * $waterText 文字水印,即把文字作为为水印,支持ASCII码,不支持中文;
  11. * $textFont 文字大小,值为1、2、3、4或5,默认为5;
  12. * $textColor 文字颜色,值为十六进制颜色值,默认为#FF0000(红色);
  13. *
  14. * 注意:Support GD 2.0,Support FreeType、GIF Read、GIF Create、JPG 、PNG
  15. * $waterImage 和 $waterText 最好不要同时使用,选其中之一即可,优先使用 $waterImage。
  16. * 当$waterImage有效时,参数$waterString、$stringFont、$stringColor均不生效。
  17. * 加水印后的图片的文件名和 $groundImage 一样。
  18. * 作者:longware @ 2004-11-3 14:15:13
  19. */
  20. function imageWaterMark($groundImage,$waterPos=0,$waterImage=”",$waterText=”",$textFont=5,$textColor=”#FF0000″)
  21. {
  22. $isWaterImage = FALSE;
  23. $formatMsg = “暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。”;
  24. //读取水印文件
  25. if(!emptyempty($waterImage) && file_exists($waterImage))
  26. {
  27. $isWaterImage = TRUE;
  28. $water_info = getimagesize($waterImage);
  29. $water_w = $water_info[0];//取得水印图片的宽
  30. $water_h = $water_info[1];//取得水印图片的高
  31. switch($water_info[2])//取得水印图片的格式
  32. {
  33. case 1:$water_im = imagecreatefromgif($waterImage);break;
  34. case 2:$water_im = imagecreatefromjpeg($waterImage);break;
  35. case 3:$water_im = imagecreatefrompng($waterImage);break;
  36. default:die($formatMsg);
  37. }
  38. }
  39. //读取背景图片
  40. if(!emptyempty($groundImage) && file_exists($groundImage))
  41. {
  42. $ground_info = getimagesize($groundImage);
  43. $ground_w = $ground_info[0];//取得背景图片的宽
  44. $ground_h = $ground_info[1];//取得背景图片的高
  45. switch($ground_info[2])//取得背景图片的格式
  46. {
  47. case 1:$ground_im = imagecreatefromgif($groundImage);break;
  48. case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
  49. case 3:$ground_im = imagecreatefrompng($groundImage);break;
  50. default:die($formatMsg);
  51. }
  52. }
  53. else
  54. {
  55. die(”需要加水印的图片不存在!”);
  56. }
  57. //水印位置
  58. if($isWaterImage)//图片水印
  59. {
  60. $w = $water_w;
  61. $h = $water_h;
  62. $label = “图片的”;
  63. }
  64. else//文字水印
  65. {
  66. $temp = imagettfbbox(ceil($textFont*5),0,”./cour.ttf”,$waterText);//取得使用 TrueType 字体的文本的范围
  67. $w = $temp[2] - $temp[6];
  68. $h = $temp[3] - $temp[7];
  69. unset($temp);
  70. $label = “文字区域”;
  71. }
  72. if( ($ground_w{
  73. echo “需要加水印的图片的长度或宽度比水印”.$label.”还小,无法生成水印!”;
  74. return;
  75. }
  76. switch($waterPos)
  77. {
  78. case 0://随机
  79. $posX = rand(0,($ground_w - $w));
  80. $posY = rand(0,($ground_h - $h));
  81. break;
  82. case 1://1为顶端居左
  83. $posX = 0;
  84. $posY = 0;
  85. break;
  86. case 2://2为顶端居中
  87. $posX = ($ground_w - $w) / 2;
  88. $posY = 0;
  89. break;
  90. case 3://3为顶端居右
  91. $posX = $ground_w - $w;
  92. $posY = 0;
  93. break;
  94. case 4://4为中部居左
  95. $posX = 0;
  96. $posY = ($ground_h - $h) / 2;
  97. break;
  98. case 5://5为中部居中
  99. $posX = ($ground_w - $w) / 2;
  100. $posY = ($ground_h - $h) / 2;
  101. break;
  102. case 6://6为中部居右
  103. $posX = $ground_w - $w;
  104. $posY = ($ground_h - $h) / 2;
  105. break;
  106. case 7://7为底端居左
  107. $posX = 0;
  108. $posY = $ground_h - $h;
  109. break;
  110. case 8://8为底端居中
  111. $posX = ($ground_w - $w) / 2;
  112. $posY = $ground_h - $h;
  113. break;
  114. case 9://9为底端居右
  115. $posX = $ground_w - $w;
  116. $posY = $ground_h - $h;
  117. break;
  118. default://随机
  119. $posX = rand(0,($ground_w - $w));
  120. $posY = rand(0,($ground_h - $h));
  121. break;
  122. }
  123. //设定图像的混色模式
  124. imagealphablending($ground_im, true);
  125. if($isWaterImage)//图片水印
  126. {
  127. imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷贝水印到目标文件
  128. }
  129. else//文字水印
  130. {
  131. if( !emptyempty($textColor) && (strlen($textColor)==7) )
  132. {
  133. $R = hexdec(substr($textColor,1,2));
  134. $G = hexdec(substr($textColor,3,2));
  135. $B = hexdec(substr($textColor,5));
  136. }
  137. else
  138. {
  139. die(”水印文字颜色格式不正确!”);
  140. }
  141. imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
  142. }
  143. //生成水印后的图片
  144. @unlink($groundImage);
  145. switch($ground_info[2])//取得背景图片的格式
  146. {
  147. case 1:imagegif($ground_im,$groundImage);break;
  148. case 2:imagejpeg($ground_im,$groundImage);break;
  149. case 3:imagepng($ground_im,$groundImage);break;
  150. default:die($errorMsg);
  151. }
  152. //释放内存
  153. if(isset($water_info)) unset($water_info);
  154. if(isset($water_im)) imagedestroy($water_im);
  155. unset($ground_info);
  156. imagedestroy($ground_im);
  157. }
  158. //—————————————————————————————
  159. $id=$_REQUEST['id'];
  160. $num = count($_FILES['userfile']['name']);
  161. print_r($_FILES['userfile']);
  162. print_r($_FILES['userfile']['name']);
  163. echo $num;
  164. echo “
    ”;
  165. if(isset($id)){
  166. for($i=0;$iif(isset($_FILES) && !emptyempty($_FILES['userfile']) && $_FILES['userfile']['size']>0)
  167. {
  168. $uploadfile = “./”.time().”_”.$_FILES['userfile'][name][$i];
  169. echo “
    ”;
  170. echo $uploadfile;
  171. if (copy($_FILES['userfile']['tmp_name'][$i], $uploadfile))
  172. {
  173. echo “OK
    ”;
  174. //文字水印
  175. //imageWaterMark($uploadfile,5,”",”HTTP://www.lvye.info”,5,”#cccccc“);
  176. //图片水印
  177. $waterImage=”logo_ok1.gif”;//水印图片路径
  178. imageWaterMark($uploadfile,9,$waterImage);
  179. echo “php图片水印类,php添加中文水印代码 ”;
  180. }
  181. else
  182. {
  183. echo “Fail
    ”;
  184. }
  185. }
  186. }
  187. }
  188. ?>
  189. for($a=0;$aecho “文件:
    ”;
  190. }
  191. ?>
复制代码

代码4 增加中文水印

  1. /*-----
  2. **描述:这是用于给指定图片加底部水印(不占用图片显示区域)的自定义类,需创建对象调用
  3. **创建:2007-10-09
  4. **更新:2007-10-09
  5. **说明:1、需要gd库支持,需要iconv支持(php5已经包含不用加载)
  6. 2、只适合三种类型的图片,jpg/jpeg/gif/png,其它类型不处理
  7. 3、注意图片所在目录的属性必须可写
  8. 4、调用范例:
  9. $objImg = new MyWaterDownChinese();
  10. $objImg->Path = "images/";
  11. $objImg->FileName = "1.jpg";
  12. $objImg->Text = "HAHAKONGJIANHTTP://HI.BAIDU.COM/LYSONCN";
  13. $objImg->Font = "./font/simhei.ttf";
  14. $objImg->Run();
  15. **成员函数:
  16. --------------*/
  17. class MyWaterDownChinese{
  18. var $Path = "./"; //图片所在目录相对于调用此类的页面的相对路径
  19. var $FileName = ""; //图片的名字,如“1.jpg”
  20. var $Text = ""; //图片要加上的水印文字,支持中文
  21. var $TextColor = "#ffffff"; //文字的颜色,gif图片时,字体颜色只能为黑色
  22. var $TextBgColor = "#000000"; //文字的背景条的颜色
  23. var $Font = "c://windows//fonts//simhei.ttf"; //字体的存放目录,相对路径
  24. var $OverFlag = true; //是否要覆盖原图,默认为覆盖,不覆盖时,自动在原图文件名后+"_water_down",如“1.jpg”=> "1_water_down.jpg"
  25. var $BaseWidth = 200; //图片的宽度至少要>=200,才会加上水印文字。
  26. //--------------
  27. //功能:类的构造函数(php5.0以上的形式)
  28. //参数:无
  29. //返回:无
  30. function __construct(){;}
  31. //------------------------
  32. //功能:类的析构函数(php5.0以上的形式)
  33. //参数:无
  34. //返回:无
  35. function __destruct(){;}
  36. //----------------------
  37. //功能:对象运行函数,给图片加上水印
  38. //参数:无
  39. //返回:无
  40. function Run()
  41. {
  42. if($this->FileName == "" || $this->Text == "")
  43. return;
  44. //检测是否安装GD库
  45. if(false == function_exists("gd_info"))
  46. {
  47. echo "系统没有安装GD库,不能给图片加水印.";
  48. return;
  49. }
  50. //设置输入、输出图片路径名
  51. $arr_in_name = explode(".",$this->FileName);
  52. //
  53. $inImg = $this->Path.$this->FileName;
  54. $outImg = $inImg;
  55. $tmpImg = $this->Path.$arr_in_name[0]."_tmp.".$arr_in_name[1]; //临时处理的图片,很重要
  56. if(!$this->OverFlag)
  57. $outImg = $this->Path.$arr_in_name[0]."_water_down.".$arr_in_name[1];
  58. //检测图片是否存在
  59. if(!file_exists($inImg))
  60. return ;
  61. //获得图片的属性
  62. $groundImageType = @getimagesize($inImg);
  63. $imgWidth = $groundImageType[0];
  64. $imgHeight = $groundImageType[1];
  65. $imgType = $groundImageType[2];
  66. if($imgWidth BaseWidth) //小于基本宽度,不处理
  67. return;
  68. //图片不是jpg/jpeg/gif/png时,不处理
  69. switch($imgType)
  70. {
  71. case 1:
  72. $image = imagecreatefromgif($inImg);
  73. $this->TextBgColor = "#ffffff"; //gif图片字体只能为黑,所以背景颜色就设置为白色
  74. break;
  75. case 2:
  76. $image = imagecreatefromjpeg($inImg);
  77. break;
  78. case 3:
  79. $image = imagecreatefrompng($inImg);
  80. break;
  81. default:
  82. return;
  83. break;
  84. }
  85. //创建颜色
  86. $color = @imagecolorallocate($image,hexdec(substr($this->TextColor,1,2)),hexdec(substr($this->TextColor,3,2)),hexdec(substr($this->TextColor,5,2))); //文字颜色
  87. //生成一个空的图片,它的高度在底部增加水印高度
  88. $newHeight = $imgHeight+20;
  89. $objTmpImg = @imagecreatetruecolor($imgWidth,$newHeight);
  90. $colorBg = @imagecolorallocate($objTmpImg,hexdec(substr($this->TextBgColor,1,2)),hexdec(substr($this->TextBgColor,3,2)),hexdec(substr($this->TextBgColor,5,2))); //背景颜色
  91. //填充图片的背景颜色
  92. @imagefill ($objTmpImg,0,0,$colorBg);
  93. //把原图copy到临时图片中
  94. @imagecopy($objTmpImg,$image,0,0,0,0,$imgWidth,$imgHeight);
  95. //创建要写入的水印文字对象
  96. $objText = $this->createText($this->Text);
  97. //计算要写入的水印文字的位置
  98. $x = 5;
  99. $y = $newHeight-5;
  100. //写入文字水印
  101. @imagettftext($objTmpImg,10,0,$x,$y,$color,$this->Font,$objText);
  102. //生成新的图片,临时图片
  103. switch($imgType)
  104. {
  105. case 1:
  106. imagegif($objTmpImg,$tmpImg);
  107. break;
  108. case 2:
  109. imagejpeg($objTmpImg,$tmpImg);
  110. break;
  111. case 3:
  112. imagepng($objTmpImg,$tmpImg);
  113. break;
  114. default:
  115. return;
  116. break;
  117. }
  118. //释放资源
  119. @imagedestroy($objTmpImg);
  120. @imagedestroy($image);
  121. //重新命名文件
  122. if($this->OverFlag)
  123. {
  124. //覆盖原图
  125. @unlink($inImg);
  126. @rename($tmpImg,$outImg);
  127. }
  128. else
  129. {
  130. //不覆盖原图
  131. @rename($tmpImg,$outImg);
  132. }
  133. }
  134. //----------
  135. //功能:创建水印文字对象
  136. //参数:无
  137. //返回:创建的水印文字对象
  138. function createText($instring)
  139. {
  140. $outstring="";
  141. $max=strlen($instring);
  142. for($i=0;$i{
  143. $h=ord($instring[$i]);
  144. if($h>=160 && $i{
  145. $outstring .= "".base_convert(bin2hex(iconv("gb2312","ucs-2",substr($instring,$i,2))),16,10).";";
  146. $i++;
  147. }
  148. else
  149. {
  150. $outstring .= $instring[$i];
  151. }
  152. }
  153. return $outstring;
  154. }
  155. }//class
  156. ?>
复制代码


source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!