PHP image Watermark class, PHP add Chinese watermark Code

Source: Internet
Author: User
Tags add watermark to picture imagecopy imagejpeg
    1. Header ("Content-type:image/png"); /* Notify browser, to output image */
    2. $im = Imagecreate (400, 300); /* Define the size of the image */
    3. $gray = Imagecolorallocate ($im, 235, 235, 235);
    4. $pink = Imagecolorallocate ($im, 255, 128, 255);
    5. $fontfile = "Simkai.ttf";
    6. /* $fontfile the path to the font, depending on the operating system, can be Simhei.ttf (bold), Simkai. TTF (in italics), Simfang. TTF (imitation), SimSun. TTC (Arial & XXFarEastFont-Arial) and other GD-supported Chinese fonts */
    7. $str = Iconv (' GB2312 ', ' UTF-8 ', ' Chinese watermark '); /* Convert the gb2312 character set to UTF-8 characters */
    8. Imagettftext ($im, 0, $pink, $fontfile, $STR);
    9. /* Add Chinese watermark */
    10. Imagepng ($im);
    11. Imagedestroy ($im);
    12. ?>
Copy Code

Example 2,php image watermark code.

  1. // **************************************** //
  2. Function: Add text to a picture
  3. Parameters: $img picture file name
  4. $new _img Save picture file name, if empty indicates no picture is saved
  5. $text string Content
  6. Text_size string Size
  7. Text_angle Font string Output angle
  8. text_x string output x-coordinate
  9. text_y string output y-coordinate
  10. $text _font Font Name
  11. $r, $g, $b string color RGB value
  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 ("Open picture Failed!");
  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. This function writes the TTF (TrueType Fonts) font text to the picture.
  20. Parameter: size is the dimension of the glyph;
  21. Angle for the angle of the font, clockwise calculation, 0 degrees to the horizontal (from left to right), 90 degrees from the bottom to the top of the text;
  22. The x, y two parameter is the coordinate value of the text (the origin is the upper left corner);
  23. Col is the color of the word;
  24. Fontfile is the font file name;
  25. Text is the string content.
  26. Imagettftext ($im, $text _size, $text _angle, $text _x, $text _y, $color, $text _font, $text);
  27. if ($new _img== ""):
  28. Imagegif ($im); Do not save pictures, only show
  29. Else
  30. Imagegif ($im, $new _img); Save picture, but not display
  31. endif
  32. Imagedestroy ($im); End graph, free memory space
  33. }
  34. ?>
Copy Code

Example 3,php image watermark, support PHP text watermark effect.

  1. /*
  2. * Function: PHP image watermark (watermark supports picture or text)
  3. Parameters
  4. * $groundImage background image, that is, need to add a watermark image, temporarily only support gif,jpg,png format;
  5. * $waterPos watermark position, there are 10 kinds of states, 0 is a random position;
  6. * 1 for the top left, 2 for the top center, 3 for the top right;
  7. * 4 for the middle of the left, 5 for the Middle center, 6 for the middle right;
  8. * 7 for the bottom of the left, 8 for the bottom center, 9 for the bottom of the right;
  9. * $waterImage image watermark, that is, as a watermark image, temporarily only support gif,jpg,png format;
  10. * $waterText text watermark, that is, the text as a watermark, support ASCII code, does not support Chinese;
  11. * $textFont Text size, value 1, 2, 3, 4 or 5, default is 5;
  12. * $textColor text color, value is hexadecimal color value, default is #ff0000 (red);
  13. *
  14. * Note: Support GD 2.0,support FreeType, gif Read, GIF Create, JPG, PNG
  15. * $waterImage and $waterText best not to use at the same time, choose one of them, priority to use the $waterImage.
  16. * When the $waterimage is valid, the parameters $waterstring, $stringFont, $stringColor are not effective.
  17. * The watermark image has the same file name as the $groundImage.
  18. * Author: 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 = "Does not support this file format, please use the picture processing software to convert the picture to GIF, JPG, PNG format. ”;
  24. Read Watermark File
  25. if (!emptyempty ($waterImage) && file_exists ($waterImage))
  26. {
  27. $isWaterImage = TRUE;
  28. $water _info = getimagesize ($waterImage);
  29. $water _w = $water _info[0];//To obtain the width of the watermark picture
  30. $water _h = $water _info[1];//To obtain a high watermark image
  31. Switch ($water _info[2])//Get the format of the watermark picture
  32. {
  33. Case 1: $water _im = Imagecreatefromgif ($waterImage);
  34. Case 2: $water _im = Imagecreatefromjpeg ($waterImage);
  35. Case 3: $water _im = Imagecreatefrompng ($waterImage);
  36. Default:die ($FORMATMSG);
  37. }
  38. }
  39. Read background picture
  40. if (!emptyempty ($groundImage) && file_exists ($groundImage))
  41. {
  42. $ground _info = getimagesize ($groundImage);
  43. $ground _w = $ground _info[0];//Get the width of the background image
  44. $ground _h = $ground _info[1];//get a high background picture
  45. Switch ($ground _info[2])//Get the format of the background picture
  46. {
  47. Case 1: $ground _im = Imagecreatefromgif ($groundImage);
  48. Case 2: $ground _im = Imagecreatefromjpeg ($groundImage);
  49. Case 3: $ground _im = Imagecreatefrompng ($groundImage);
  50. Default:die ($FORMATMSG);
  51. }
  52. }
  53. Else
  54. {
  55. Die ("The picture that needs watermark does not exist!") ”);
  56. }
  57. Watermark Location
  58. if ($isWaterImage)//Picture watermark
  59. {
  60. $w = $water _w;
  61. $h = $water _h;
  62. $label = "Picture";
  63. }
  64. else//text watermark
  65. {
  66. $temp = Imagettfbbox (Ceil ($textFont), 0, "./cour.ttf", $waterText);//Get the range of text that uses TrueType fonts
  67. $w = $temp [2]-$temp [6];
  68. $h = $temp [3]-$temp [7];
  69. Unset ($temp);
  70. $label = "Text Area";
  71. }
  72. if ($ground _w< $w) | | ($ground _h< $h))
  73. {
  74. echo "The length or width of the picture that needs to be watermarked is more than a watermark". $label. " Still small, unable to generate watermark! ”;
  75. Return
  76. }
  77. Switch ($waterPos)
  78. {
  79. Case 0://Random
  80. $posX = rand (0, ($ground _w-$w));
  81. $posY = rand (0, ($ground _h-$h));
  82. Break
  83. Case 1://1 for top left
  84. $posX = 0;
  85. $posY = 0;
  86. Break
  87. Case 2://2 centered on the top
  88. $posX = ($ground _w-$w)/2;
  89. $posY = 0;
  90. Break
  91. Case 3://3 for top right
  92. $posX = $ground _w-$w;
  93. $posY = 0;
  94. Break
  95. Case 4://4 for the middle left
  96. $posX = 0;
  97. $posY = ($ground _h-$h)/2;
  98. Break
  99. Case 5://5 centered in the middle
  100. $posX = ($ground _w-$w)/2;
  101. $posY = ($ground _h-$h)/2;
  102. Break
  103. Case 6://6 for the middle right
  104. $posX = $ground _w-$w;
  105. $posY = ($ground _h-$h)/2;
  106. Break
  107. Case 7://7 for Bottom left
  108. $posX = 0;
  109. $posY = $ground _h-$h;
  110. Break
  111. Case 8://8 centered at bottom
  112. $posX = ($ground _w-$w)/2;
  113. $posY = $ground _h-$h;
  114. Break
  115. Case 9://9 for bottom right
  116. $posX = $ground _w-$w;
  117. $posY = $ground _h-$h;
  118. Break
  119. default://Random
  120. $posX = rand (0, ($ground _w-$w));
  121. $posY = rand (0, ($ground _h-$h));
  122. Break
  123. }
  124. To set the color blending mode of an image
  125. Imagealphablending ($ground _im, true);
  126. if ($isWaterImage)//Picture watermark
  127. {
  128. Imagecopy ($ground _im, $water _im, $posX, $posY, 0, 0, $water _w, $water _h);//copy watermark to target file
  129. }
  130. else//text watermark
  131. {
  132. if (!emptyempty ($textColor) && (strlen ($textColor) ==7))
  133. {
  134. $R = Hexdec (substr ($textColor,));
  135. $G = Hexdec (substr ($textColor, 3,2));
  136. $B = Hexdec (substr ($textColor, 5));
  137. }
  138. Else
  139. {
  140. Die ("Watermark text color format is not correct! ”);
  141. }
  142. Imagestring ($ground _im, $textFont, $posX, $posY, $waterText, Imagecolorallocate ($ground _im, $R, $G, $B));
  143. }
  144. Create a picture after a watermark
  145. @unlink ($groundImage);
  146. Switch ($ground _info[2])//Get the format of the background picture
  147. {
  148. Case 1:imagegif ($ground _im, $groundImage);
  149. Case 2:imagejpeg ($ground _im, $groundImage);
  150. Case 3:imagepng ($ground _im, $groundImage);
  151. Default:die ($ERRORMSG);
  152. }
  153. Freeing memory
  154. if (Isset ($water _info)) unset ($water _info);
  155. if (Isset ($water _im)) Imagedestroy ($water _im);
  156. unset ($ground _info);
  157. Imagedestroy ($ground _im);
  158. }
  159. —————————————————————————————
  160. $id =$_request[' id '];
  161. $num = count ($_files[' userfile ' [' name ']);
  162. Print_r ($_files[' userfile ');
  163. Print_r ($_files[' userfile ' [' name ']);
  164. Echo $num;
  165. echo "
    ”;
  166. if (Isset ($id)) {
  167. for ($i =0; $i < $id; $i + +) {
  168. if (Isset ($_files) &&!emptyempty ($_files[' userfile ']) && $_files[' userfile ' [' Size ']>0]
  169. {
  170. $uploadfile = "./". Time (). " _ ". $_files[' UserFile '][name][$i];
  171. echo "
    ”;
  172. Echo $uploadfile;
  173. if (copy ($_files[' userfile ' [' tmp_name '] [$i], $uploadfile))
  174. {
  175. echo "OK
    ”;
  176. Text watermark
  177. Imagewatermark ($uploadfile, 5, "", "HTTP://www.lvye.info", 5, "#cccccc");
  178. Image watermark
  179. $waterImage = "logo_ok1.gif";//Watermark Picture Path
  180. Imagewatermark ($uploadfile, 9, $waterImage);
  181. echo "";
  182. }
  183. Else
  184. {
  185. echo "Fail
    ”;
  186. }
  187. }
  188. }
  189. }
  190. ?>
Copy Code

Code 4 Add Chinese watermark

  1. /*-----
  2. * * Description: This is a custom class used to add a bottom watermark to the specified picture (without occupying the picture display area), and to create an object call
  3. * * Created: 2007-10-09
  4. * * Updated: 2007-10-09
  5. * * Description: 1, the need for GD library support, need ICONV support (PHP5 already included without loading)
  6. 2, only suitable for three kinds of pictures, jpg/jpeg/gif/png, other types do not handle
  7. 3. Note that the attributes of the directory in which the picture is located must be writable
  8. 4. Call Example:
  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. * * member function:
  16. --------------*/
  17. Class mywaterdownchinese{
  18. var $Path = "./"; The relative path of the directory in which the picture is located relative to the page calling this class
  19. var $FileName = ""; The name of the picture, such as "1.jpg"
  20. var $Text = ""; Watermark text to be added to the image, support Chinese
  21. var $TextColor = "#ffffff"; Text color, GIF picture, font color can only be black
  22. var $TextBgColor = "#000000"; The color of the text's background bar
  23. var $Font = "C://windows//fonts//simhei.ttf"; Font storage directory, relative path
  24. var $OverFlag = true; Whether to overwrite the original, default to overwrite, when not covered, automatically after the original file name + "_water_down", such as "1.jpg" = "1_water_down.jpg"
  25. var $BaseWidth = 200; The width of the picture must be at least >=200, and the watermark text will be added.
  26. //--------------
  27. Function: constructor of Class (Form php5.0 or more)
  28. Parameters: None
  29. return: None
  30. function __construct () {;}
  31. //------------------------
  32. Function: destructor of class (form above php5.0)
  33. Parameters: None
  34. return: None
  35. function __destruct () {;}
  36. //----------------------
  37. Function: Object run function, add watermark to Picture
  38. Parameters: None
  39. return: None
  40. function Run ()
  41. {
  42. if ($this->filename = = "" | | $this->text = = "")
  43. Return
  44. Detects if the GD library is installed
  45. if (false = = Function_exists ("Gd_info"))
  46. {
  47. echo "System does not have the GD library installed, can not add watermark to the picture.";
  48. Return
  49. }
  50. Set input, output picture path name
  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]; Temporary processing of pictures, very important
  56. if (! $this->overflag)
  57. $OUTIMG = $this->path. $arr _in_name[0]. " _water_down. ". $arr _in_name[1];
  58. Detect if a picture exists
  59. if (!file_exists ($INIMG))
  60. return;
  61. Get the properties of a picture
  62. $groundImageType = @getimagesize ($INIMG);
  63. $imgWidth = $groundImageType [0];
  64. $imgHeight = $groundImageType [1];
  65. $imgType = $groundImageType [2];
  66. if ($imgWidth < $this->basewidth)//Less than basic width, not processed
  67. Return
  68. The picture is not jpg/jpeg/gif/png when it is not processed
  69. Switch ($imgType)
  70. {
  71. Case 1:
  72. $image = Imagecreatefromgif ($INIMG);
  73. $this->textbgcolor = "#ffffff"; GIF picture font can only be black, so the background color is set to white
  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. Create color
  86. $color = @imagecolorallocate ($image, Hexdec (substr ($this->textcolor,1,2)), Hexdec (substr ($this->textcolor, 3,2)), Hexdec (substr ($this->textcolor,5,2))); Text color
  87. Generate an empty picture, its height increases the watermark height at the bottom
  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))); Background color
  91. Fill the background color of the picture
  92. @imagefill ($OBJTMPIMG, 0,0, $colorBg);
  93. Copy the original to a temporary image
  94. @imagecopy ($OBJTMPIMG, $image, 0,0,0,0, $imgWidth, $imgHeight);
  95. Create a watermark text object to write to
  96. $objText = $this->createtext ($this->text);
  97. Calculate the location of the watermark text to be written
  98. $x = 5;
  99. $y = $newHeight-5;
  100. Write a text watermark
  101. @imagettftext ($OBJTMPIMG, 10,0, $x, $y, $color, $this->font, $objText);
  102. Create a new picture, temporary picture
  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. Freeing resources
  119. @imagedestroy ($OBJTMPIMG);
  120. @imagedestroy ($image);
  121. Renaming files
  122. if ($this->overflag)
  123. {
  124. Overwrite the original
  125. @unlink ($INIMG);
  126. @rename ($TMPIMG, $OUTIMG);
  127. }
  128. Else
  129. {
  130. Do not overwrite the original
  131. @rename ($TMPIMG, $OUTIMG);
  132. }
  133. }
  134. //----------
  135. Function: Create watermark Text Object
  136. Parameters: None
  137. Return: Created Watermark text Object
  138. function CreateText ($instring)
  139. {
  140. $outstring = "";
  141. $max =strlen ($instring);
  142. for ($i =0; $i < $max; $i + +)
  143. {
  144. $h =ord ($instring [$i]);
  145. if ($h >=160 && $i < $max-1)
  146. {
  147. $outstring. = "Base_convert" (Bin2Hex (Iconv ("gb2312", "Ucs-2", substr ($instring, $i, 2)), 16,10). ";";
  148. $i + +;
  149. }
  150. Else
  151. {
  152. $outstring. = $instring [$i];
  153. }
  154. }
  155. return $outstring;
  156. }
  157. }//class
  158. ?>
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.