Example of adding a watermark to a php image

Source: Internet
Author: User
Example of adding a watermark to a php image

Example: php code used to add a watermark to an image.

  1. /*
  2. // Example
  3. $ Image = new Gimage ();
  4. $ Image-> limit = 600; // maximum length and width
  5. $ Image-> wm_text = "www.linuxlaptop.cn"; // watermark text
  6. $ Image-> wm_fontfile = "font/xsuni. ttf"; // font file
  7. $ Image-> wm_color = "# ff0000 ″;
  8. $ Image-> save_file = export ltcn.jpg "; // save to xx file
  9. $ Image-> create(“linuxlaptop.jpg "); // create a file from xx
  10. */
  11. /*
  12. + ------------------------------
  13. | Create a thumbnail and watermark image
  14. + ------------------------------
  15. */
  16. Class Gimage {
  17. Var $ input_type = ""; // format of the input image
  18. Var $ output_type = "jpg"; // output Image format
  19. Var $ limit = 0; // image size limit
  20. Var $ filename = ""; // enter the image file name (or image data)
  21. Var $ pai_quality = 90; // jpeg image quality
  22. Var $ save_file = ''; // output file name
  23. Var $ wm_text = ""; // watermark text (Chinese: '() not supported :'()
  24. Var $ wm_size = 12; // watermark text size
  25. Var $ wm_angle = 0; // watermark text angle
  26. Var $ wm_x = 30; // watermark x coordinate
  27. Var $ wm_y = 30; // y coordinate of the watermark
  28. Var $ wm_color = "# cccccc"; // watermark color
  29. Var $ wm_fontfile = "geodesic. ttf"; // watermark font file
  30. Function create ($ filename = "")
  31. {
  32. If ($ filename) $ this-> filename = $ filename;
  33. If (! $ This-> input_type) $ this-> get_type ();
  34. If (! $ This-> output_type) $ this-> output_type = $ this-> input_type;
  35. If ($ this-> input_type = "jpg") $ this-> input_type = "jpeg ";
  36. If ($ this-> output_type = "jpg") $ this-> output_type = "jpeg ";
  37. Switch ($ this-> input_type ){
  38. Case 'GIF ':
  39. $ Src_img = ImageCreateFromGIF ($ this-> filename );
  40. Break;
  41. Case 'jpeg ':
  42. $ Src_img = ImageCreateFromJPEG ($ this-> filename );
  43. Break;
  44. Case 'PNG ':
  45. $ Src_img = ImageCreateFromPNG ($ this-> filename );
  46. Break;
  47. Default:
  48. $ Src_img = ImageCreateFromString ($ this-> filename );
  49. Break;
  50. }
  51. $ Src_w = ImageSX ($ src_img );
  52. $ Src_h = ImageSY ($ src_img );
  53. If ($ src_w >=$ src_h ){
  54. If ($ src_w> $ this-> limit ){
  55. $ New_w = $ this-> limit;
  56. $ New_h = ($ this-> limit/$ src_w) * $ src_h;
  57. }
  58. }
  59. Else {
  60. If ($ src_h> $ this-> limit ){
  61. $ New_h = $ this-> limit;
  62. $ New_w = ($ this-> limit/$ src_h) * $ src_w;
  63. }
  64. }
  65. If ($ new_h ){
  66. $ Dst_img = imagecreatetruecolor ($ new_w, $ new_h );
  67. Imagecopyresampled ($ dst_img, $ src_img, 0, 0, 0, $ new_w, $ new_h, ImageSX ($ src_img), ImageSY ($ src_img ));
  68. }
  69. Else {
  70. $ Dst_img = $ src_img;
  71. }
  72. If ($ this-> wm_text)
  73. {
  74. If (preg_match ("/([a-f0-9] [a-f0-9]) ([a-f0-9] [a-f0-9]) ([a-f0-9])/I", $ this-> wm_color, $ color ))
  75. {
  76. $ Red = hexdec ($ color [1]);
  77. $ Green = hexdec ($ color [2]);
  78. $ Blue = hexdec ($ color [3]);
  79. }
  80. $ Wm_color = imagecolorallocatealpha ($ dst_img, $ red, $ green, $ blue, 90 );
  81. Imagettftext ($ dst_img, $ this-> wm_size, $ this-> wm_angle, $ this-> wm_x, $ this-> wm_y, $ wm_color, $ this-> wm_fontfile, $ this-> wm_text );
  82. }
  83. If ($ this-> save_file)
  84. {
  85. Switch ($ this-> output_type ){
  86. Case 'GIF ':
  87. $ Src_img = ImagePNG ($ dst_img, $ this-> save_file );
  88. Break;
  89. Case 'jpeg ':
  90. $ Src_img = ImageJPEG ($ dst_img, $ this-> save_file, $ this-> 1__quality );
  91. Break;
  92. Case 'PNG ':
  93. $ Src_img = ImagePNG ($ dst_img, $ this-> save_file );
  94. Break;
  95. Default:
  96. $ Src_img = ImageJPEG ($ dst_img, $ this-> save_file, $ this-> 1__quality );
  97. Break;
  98. }
  99. }
  100. Else
  101. {
  102. Header ("Content-type: image/{$ this-> output_type }");
  103. Switch ($ this-> output_type ){
  104. Case 'GIF ':
  105. $ Src_img = ImagePNG ($ dst_img );
  106. Break;
  107. Case 'jpeg ':
  108. $ Src_img = ImageJPEG ($ dst_img, "", $ this-> pai_quality );
  109. Break;
  110. Case 'PNG ':
  111. $ Src_img = ImagePNG ($ dst_img );
  112. Break;
  113. Default:
  114. $ Src_img = ImageJPEG ($ dst_img, "", $ this-> pai_quality );
  115. Break;
  116. }
  117. }
  118. Imagedestroy ($ dst_img );
  119. }
  120. Function get_type () // Obtain the image file type
  121. {
  122. $ Name_array = explode (".", $ this-> filename );
  123. If (preg_match ("/\. (jpg | jpeg | gif | png) $/", $ this-> filename, $ matches ))
  124. {
  125. $ This-> input_type = strtolower ($ matches [1]);
  126. }
  127. Else
  128. {
  129. $ This-> input_type = "string ";
  130. }
  131. }
  132. }

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.