Added the Image Upload watermark function (php Code) to the Kindeditor editor ),

Source: Internet
Author: User

Added the Image Upload watermark function (php Code) to the Kindeditor editor ),

KindEditor is an open-source online HTML editor. developers can use KindEditor to replace the traditional multi-line text input box (textarea) with a visualized rich text input box. It can be seamlessly integrated with Java,. NET, PHP, ASP and other programs, and is suitable for use in Internet applications such as CMS, mall, forum, blog, Wiki, and email.
Main features:

Fast: small size, fast loading

Open Source: open source code, high level, high quality

Underlying layer: built-in custom DOM class library for precise DOM operations

Extension: plug-in-based design. All functions are plug-ins. You can add or remove functions as needed.

Style: it is very easy to modify the editor style. You only need to modify a CSS file.

Compatible: supports most mainstream browsers, such as IE, Firefox, Safari, Chrome, and Opera.

Go to the topic. The default editor does not have the watermark function when uploading images. the following details:

Step 1:Modify the upload_json.php File

You can find this file in the/php/directory of the editor and add a function:

/** Function: PHP image watermark. The watermark supports image or text. * parameter: * $ groundImage background image, that is, the image to be watermarked. Currently, only GIF, JPG, and PNG formats are supported; * $ waterPos watermark position. There are 10 States. 0 indicates a random position. * 1 indicates the top center, 2 indicates the top center, and 3 indicates the top center. * 4 indicates the center, 5: Center in the middle, 6: center in the right; * 7: center at the bottom, 8: center at the bottom, 9: right at the bottom; * $ waterImage image watermark, that is, the image used as the watermark, currently, only the GIF, JPG, and PNG formats are supported. * $ alpha watermark transparency; value range: 1-100; * $ waterText text watermark. Text is used as a watermark and ASCII code is supported, chinese characters are not supported; * $ textFont text size; values: 1, 2, 3, 4, or 5; default value: 5; * $ textColor text color; Value: hexadecimal color value, the default value is # FF0000 (red); ** $ waterImage and $ wat ErText is better not to be used at the same time. select one of them and use $ waterImage first. * When $ waterImage is valid, $ waterString, $ stringFont, and $ stringColor are invalid. * The Name Of The watermark image is the same as that of $ groundImage. */Function imageWaterMark ($ groundImage, $ waterPos = 0, $ waterImage = '', $ alpha = 80, $ waterText ='', $ water_fontfile, $ textFont = 9, $ textColor = '# ff0000') {$ isWaterImage = FALSE; $ formatMsg =' this image format is not supported! Use images in GIF, JPG, and PNG formats. '; $ FontFile = $ water_fontfile; // read the watermark file if (! Empty ($ waterImage) & file_exists ($ waterImage) {$ isWaterImage = TRUE; $ water_info = getimagesize ($ waterImage); $ water_w = $ water_info [0]; // get the width of the watermark image $ water_h = $ water_info [1]; // get the height switch of the watermark image ($ water_info [2]) {// watermark image format: case 1: $ water_im = imagecreatefromgif ($ waterImage); break; case 2: $ water_im = imagecreatefromjpeg ($ waterImage); break; case 3: $ water_im = imagecreatefrompng ($ waterImage); break; defau Lt: die ($ formatMsg) ;}// read the background image if (! Empty ($ groundImage) & file_exists ($ groundImage) {$ ground_info = getimagesize ($ groundImage); $ ground_w = $ ground_info [0]; // get the width of the background image $ ground_h = $ ground_info [1]; // get the height switch of the background image ($ ground_info [2]) {// format of the background image: case 1: $ ground_im = imagecreatefromgif ($ groundImage); break; case 2: $ ground_im = imagecreatefromjpeg ($ groundImage); break; case 3: $ ground_im = imagecreatefrompng ($ groundImage); break; default: die ($ FormatMsg) ;}} else {alert ("the watermark image does not exist! ");} // Watermark position if ($ isWaterImage) {// image watermark $ w = $ water_w; $ h = $ water_h; $ label =" image ";} else {// text watermark $ temp = imagettfbbox ($ textFont, 0, $ fontFile, $ waterText ); // obtain the text range using the TrueType font $ w = $ temp [2]-$ temp [6]; $ h = $ temp [3]-$ temp [7]; unset ($ temp); $ label = "text area";} if ($ ground_w <$ w) | ($ ground_h <$ h )) {echo "watermark image length or width ratio ". $ label. "The watermark cannot be generated because it is small! "; Return;} switch ($ waterPos) {case 0: // random $ posX = rand (0, ($ ground_w-$ w); $ posY = rand (0, ($ ground_h-$ h); break; case 1: // 1 is the top left corner. $ posX = 0; $ posY = 0; break; case 2: // 2 is centered at the top $ posX = ($ ground_w-$ w)/2; $ posY = 0; break; case 3: // 3: top-Right: $ posX = $ ground_w-$ w; $ posY = 0; break; case 4: // 4: center-left: $ posX = 0; $ posY = ($ ground_h-$ h)/2; break; case 5: // 5 center in the middle $ posX = ($ ground_w-$ w)/2; $ posY = ($ Ground_h-$ h)/2; break; case 6: // 6 is the center right $ posX = $ ground_w-$ w; $ posY = ($ ground_h-$ h)/2; break; case 7: // 7 is left at the bottom $ posX = 0; $ posY = $ ground_h-$ h; break; case 8: // 8 is centered at the bottom $ posX = ($ ground_w-$ w)/2; $ posY = $ ground_h-$ h; break; case 9: // 9 is left at the bottom of the page $ posX = $ ground_w-$ w; $ posY = $ ground_h-$ h; if (! $ IsWaterImage) {$ posY = $ ground_h-$ h-20;} break; default: // random $ posX = rand (0, ($ ground_w-$ w )); $ posY = rand (0, ($ ground_h-$ h); break;} // sets the image color mixing mode imagealphablending ($ ground_im, true); if ($ isWaterImage) {// image watermark // imagecopy ($ ground_im, $ water_im, $ posX, $ posY, 0, 0, $ water_w, $ water_h ); // copy the watermark to the target file // generate a hybrid image imagecopymerge ($ ground_im, $ water_im, $ posX, $ posY, 0, 0, $ water_w, $ water_h, $ alpha );} Else {// text watermark if (! Empty ($ textColor) & (strlen ($ textColor) = 7) {$ R = hexdec (substr ($ textColor, 1, 2 )); $ G = hexdec (substr ($ textColor, 3, 2); $ B = hexdec (substr ($ textColor, 5 ));} else {die ("the watermark text color format is incorrect! ");} Imagestring ($ ground_im, $ textFont, $ posX, $ posY, $ waterText, imagecolorallocate ($ ground_im, $ R, $ G, $ B ));} // The image after the watermark is generated @ unlink ($ groundImage); switch ($ ground_info [2]) {// The format of case 1: imagegif ($ ground_im, $ groundImage); break; case 2: imagejpeg ($ ground_im, $ groundImage, 100); break; // note that 100 is the best image quality, but the file size will increase a lot. The quality is good when 95, and the size is similar to the original one. The author uses 95 values. Case 3: imagepng ($ ground_im, $ groundImage); break; default: die ($ errorMsg);} // release the memory if (isset ($ water_info )) unset ($ water_info); if (isset ($ water_im) imagedestroy ($ water_im); unset ($ ground_info); imagedestroy ($ ground_im );}

Step 2:Find $ json = new Services_JSON (). Note that there are two places, not the one in the alert function. Add the following code:

/* Watermark configuration start */$ water_mark = 1; // 1 indicates watermarking, and other conditions include $ water_pos = 9. // watermark position. The value 0 indicates random, 1: top left, 2: Top center, 3: top right, 4: center left, 5: Center, 6: center right, 7: bottom left, 8: center at the bottom and 9: bottom. $ water_img = $ _ SERVER ['document _ root']. '/upfiles/water.gif'; // watermark image. It is blank by default. Upload the image to the upfiles directory in the root directory of the website, for example, water.gif $ water_alpha = 50; // watermark transparency $ water_text = ''; // watermark string, empty by default; // $ water_fontfile = $ _ SERVER ['document _ root']. '/upfiles/fonts/arial. ttf'; // The font used by the text watermark; if ($ water_mark = 1) {imageWaterMark ($ file_path, $ water_pos, $ water_img, $ water_alpha, $ water_text, $ water_fontfile);}/* watermark configuration ends */

After testing, you can use the image watermark properly. Pay attention to the path of the watermark image, depending on the actual situation.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.