Add watermark for Baidu Ueditor editor upload image

Source: Internet
Author: User

Form:http://www.uphtm.com/php/255.html

Ueditor Editor upload image is automatically extracted, but the picture does not have a watermark function, the following small series and you see together.

Ueditor Editor does not upload images and watermark function, need to carry out two development, this example is in the PHPCMS system Baidu Editor two times to develop, add upload images and watermark function.


First open the Ueditor Editor file directory of the PHP folder, open the Uploader.class.php, the Phpcms watermark method to copy, add to this class all member methods of the last side, and then modify the following:

  1. Picture plus watermark
  2. Public function watermark ($source, $target = ", $w _pos =", $w _img = ", $w _text = ' 99danji ', $w _font = 8, $w _color = ' #f f0000 ') {
  3. $this->w_img = ' watermark.png ';
  4. $this->w_pos = 9;
  5. $this->w_minwidth = 400;
  6. $this->w_minheight = 200;
  7. $this->w_quality = 80;
  8. $this->w_pct = 85;
  9. $w _pos = $w _pos? $w _pos: $this->w_pos;
  10. $w _img = $w _img? $w _img: $this->w_img;
  11. if (! $this->watermark_enable | |! $this->check ($source)) return false;
  12. if (! $target) $target = $source;
  13. $w _img = Phpcms_path. $w _img;
  14. Define (' Www_path ', DirName (DirName (dirname (__file__)));
  15. $w _img = '. /.. /.. /images/water/'. $w _img;
  16. $source _info = getimagesize ($source);
  17. $source _w = $source _info[0];
  18. $source _h = $source _info[1];
  19. if ($source _w < $this->w_minwidth | | $source _h < $this->w_minheight) return false;
  20. Switch ($source _info[2]) {
  21. Case 1:
  22. $source _img = imagecreatefromgif ($source);
  23. Break
  24. Case 2:
  25. $source _img = Imagecreatefromjpeg ($source);
  26. Break
  27. Case 3:
  28. $source _img = imagecreatefrompng ($source);
  29. Break
  30. Default:
  31. return false;
  32. }
  33. if (!empty ($w _img) && file_exists ($w _img)) {
  34. $ifwaterimage = 1;
  35. $water _info = getimagesize ($w _img);
  36. $width = $water _info[0];
  37. $height = $water _info[1];
  38. Switch ($water _info[2]) {
  39. Case 1:
  40. $water _img = imagecreatefromgif ($w _img);
  41. Break
  42. Case 2:
  43. $water _img = imagecreatefromjpeg ($w _img);
  44. Break
  45. Case 3:
  46. $water _img = imagecreatefrompng ($w _img);
  47. Break
  48. Default:
  49. Return
  50. }
  51. } else {
  52. $ifwaterimage = 0;
  53. $temp = Imagettfbbox (ceil ($w _font*2.5), 0, Pc_path. ' Libs/data/font/elephant.ttf ', $w _text);
  54. $width = $temp [2]-$temp [6];
  55. $height = $temp [3]-$temp [7];
  56. Unset ($temp);
  57. }
  58. Switch ($w _pos) {
  59. Case 1:
  60. $WX = 5;
  61. $wy = 5;
  62. Break
  63. Case 2:
  64. $WX = ($source _w-$width)/2;
  65. $wy = 0;
  66. Break
  67. Case 3:
  68. $WX = $source _w-$width;
  69. $wy = 0;
  70. Break
  71. Case 4:
  72. $WX = 0;
  73. $wy = ($source _h-$height)/2;
  74. Break
  75. Case 5:
  76. $WX = ($source _w-$width)/2;
  77. $wy = ($source _h-$height)/2;
  78. Break
  79. Case 6:
  80. $WX = $source _w-$width;
  81. $wy = ($source _h-$height)/2;
  82. Break
  83. Case 7:
  84. $WX = 0;
  85. $wy = $source _h-$height;
  86. Break
  87. Case 8:
  88. $WX = ($source _w-$width)/2;
  89. $wy = $source _h-$height;
  90. Break
  91. Case 9:
  92. $WX = $source _w-$width;
  93. $wy = $source _h-$height;
  94. Break
  95. Case 10:
  96. $WX = rand (0, ($source _w-$width));
  97. $wy = rand (0, ($source _h-$height));
  98. Break
  99. Default
  100. $WX = rand (0, ($source _w-$width));
  101. $wy = rand (0, ($source _h-$height));
  102. Break
  103. }
  104. if ($ifwaterimage) {
  105. if ($water _info[2] = = 3) {
  106. Imagecopy ($source _img, $water _img, $wx, $wy, 0, 0, $width, $height);
  107. } else {
  108. Imagecopymerge ($source _img, $water _img, $wx, $wy, 0, 0, $width, $height, $this->w_pct);
  109. }
  110. } else {
  111. if (!empty ($w _color) && (strlen ($w _color) ==7)) {
  112. $r = Hexdec (substr ($w _color,1,2));
  113. $g = Hexdec (substr ($w _color,3,2));
  114. $b = Hexdec (substr ($w _color,5));
  115. } else {
  116. Return
  117. }
  118. Imagestring ($source _img, $w _font, $wx, $wy, $w _text,imagecolorallocate ($source _img, $r, $g, $b));
  119. }
  120. Switch ($source _info[2]) {
  121. Case 1:
  122. Imagegif ($source _img, $target);
  123. Break
  124. Case 2:
  125. Imagejpeg ($source _img, $target, $this->w_quality);
  126. Break
  127. Case 3:
  128. Imagepng ($source _img, $target);
  129. Break
  130. Default:
  131. Return
  132. }
  133. if (Isset ($water _info)) {
  134. unset ($water _info);
  135. }
  136. if (Isset ($water _img)) {
  137. Imagedestroy ($water _img);
  138. }
  139. unset ($source _info);
  140. Imagedestroy ($source _img);
  141. return true;
  142. }
  143. Public function Check ($image) {
  144. Return extension_loaded (' gd ') && preg_match ("/\. ( jpg|jpeg|gif|png)/I ", $image, $m) && file_exists ($image) && function_exists (' Imagecreatefrom '. ( $m [1] = = ' jpg '? ' JPEG ': $m [1]);
  145. }

Compared to the part I modified, because the PHPCMS watermark can be managed in the background settings, the PHPCMS comes with the watermark method by reading the configuration file to get the path, and read the database settings to get the parameter settings, then these places need to be set manually.

By the way, the Upfile method also adds a function:

    1. if ($this->watermark) {
    2. $this->watermark ($this->filepath, $this->filepath);
    3. }

Then open the Ueditor Baidu Editor php directory under the action_upload.php file, plus whether to add the watermark parameters:

    1. /* Upload configuration */
    2. $base = "Upload";
    3. Switch (Htmlspecialchars ($_get[' action ')) {
    4. Case ' Uploadimage ':
    5. $config = Array (
    6. "Pathformat" = $CONFIG [' Imagepathformat '],
    7. "MaxSize" = $CONFIG [' Imagemaxsize '],
    8. "Allowfiles" = $CONFIG [' Imageallowfiles ']
    9. );
    10. $fieldName = $CONFIG [' Imagefieldname '];
    11. $watermark = true;
    12. Break

Then there is another sentence to be changed:

    1. /* Generate the Upload instance object and complete the upload */
    2. $up = new Uploader ($fieldName, $config, $base, $watermark);

This will be done, this article is mainly to provide ideas and reference.

Form:http://www.uphtm.com/php/255.html

Add watermark for Baidu Ueditor editor upload image

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.