Add text watermarks to existing images using php code

Source: Internet
Author: User
Add text watermarks to existing images using php code

The php class library adds text watermarks to existing images. the code is not very complete. thank you for your advice! The code is as follows:

  1. /* PHP image and text watermark class library
  2. QQ: 3697578482 sad songs
  3. Currently, this class library only supports text watermarks. the position is in the lower right corner and the color is random.
  4. Call method:
  5. 1. introduce a class library at the top of the file to be watermarked:
  6. Include_once 'imageclass. php ';
  7. 2. declare a new class:
  8. $ Tpl = new image_fu;
  9. 3. provide parameters for image watermarks:
  10. $ Tpl-> img (image path, watermark text, font path, font size, font angle );
  11. For example: $ tpl-> img('abc.jpg ', 'This is the watermark text', 'ziti. ttf)
  12. */
  13. Class image_fu {
  14. Private $ image;
  15. Private $ img_info;
  16. Private $ img_width;
  17. Private $ img_height;
  18. Private $ img_im;
  19. Private $ img_text;
  20. Private $ img_ttf = '';
  21. Private $ img_new;
  22. Private $ img_text_size;
  23. Private $ img_jd;
  24. Function img ($ img = '', $ txt ='', $ ttf = '', $ size = 12, $ jiaodu = 0 ){
  25. If (isset ($ img) & file_exists ($ img) {// check whether the image exists
  26. $ This-> image = $ img;
  27. $ This-> img_text = $ txt;
  28. $ This-> img_text_size = $ size;
  29. $ This-> img_jd = $ jiaodu;
  30. If (file_exists ($ ttf )){
  31. $ This-> img_ttf = $ ttf;
  32. } Else {
  33. Exit ('font File: '. $ ttf.' does not exist! ');
  34. }
  35. $ This-> imgyesno ();
  36. } Else {
  37. Exit ('Image File: '. $ img.' doesn't exist ');
  38. }
  39. }
  40. Private function imgyesno (){
  41. $ This-> img_info = getimagesize ($ this-> image );
  42. $ This-> img_width = $ this-> img_info [0]; // image width
  43. $ This-> img_height = $ this-> img_info [1]; // picture height
  44. // Check the image type
  45. Switch ($ this-> img_info [2]) {
  46. Case 1: $ this-> img_im = imagecreatefromgif ($ this-> image); break;
  47. Case 2: $ this-> img_im = imagecreatefromjpeg ($ this-> image); break;
  48. Case 3: $ this-> img_im = imagecreatefrompng ($ this-> image); break;
  49. Default: exit ('watermarks are not supported in image format ');
  50. }
  51. $ This-> img_text ();
  52. }
  53. Private function img_text (){
  54. Imagealphablending ($ this-> img_im, true );
  55. // Set the color
  56. $ Color = imagecolorallocate ($ this-> img_im, rand (0,255), rand (0,255), rand (0,255 ));
  57. $ Txt_height = $ this-> img_text_size;
  58. $ Txt_jiaodu = $ this-> img_jd;
  59. $ Ttf_im = imagettfbbox ($ txt_height, $ txt_jiaodu, $ this-> img_ttf, $ this-> img_text );
  60. $ W = $ ttf_im [2]-$ ttf_im [6];
  61. $ H = $ ttf_im [3]-$ ttf_im [7];
  62. // $ W = $ ttf_im [7];
  63. // $ H = $ ttf_im [8];
  64. Unset ($ ttf_im );
  65. $ Txt_y = $ this-> img_height-$ h;
  66. $ Txt_x = $ this-> img_width-$ w;
  67. // $ Txt_y = 0;
  68. // $ Txt_x = 0;
  69. $ This-> img_new = @ imagettftext ($ this-> img_im, $ txt_height, $ txt_jiaodu, $ txt_x, $ txt_y, $ color, $ this-> img_ttf, $ this-> img_text );
  70. @ Unlink ($ this-> image); // delete an image
  71. Switch ($ this-> img_info [2]) {// Obtain the background image format
  72. Case 1: imagegif ($ this-> img_im, $ this-> image); break;
  73. Case 2: imagejpeg ($ this-> img_im, $ this-> image); break;
  74. Case 3: imagepng ($ this-> img_im, $ this-> image); break;
  75. Default: exit ('watermark Image failed ');
  76. }
  77. }
  78. // Display the image
  79. Function img_show () {echo 'image. '"border =" 0 "alt ="'. $ this-> img_text. '"/> ';}
  80. // Release the memory
  81. Private function img_nothing (){
  82. Unset ($ this-> img_info );
  83. Imagedestroy ($ this-> img_im );
  84. }
  85. }
  86. ?>

Php

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.