Modify the ueditor1_4_3 editor jsp version to enable watermarks for uploaded images. ueditor1_4_3jsp

Source: Internet
Author: User

Modify the ueditor1_4_3 editor jsp version to enable watermarks for uploaded images. ueditor1_4_3jsp

Main Idea: The ueditor uploads an image and sends it to the background in the form of a request. The background receives the image and processes it in the form of a stream. This function can be implemented by intercepting the image file in the background and adding a watermark.

1. Download The ueditor1_4_3 editor jsp to make it work properly;

Ii. modify source code

Mainly modify the StorageManager. java File

/*** Merge the uploaded File and watermark File with a watermark Image */public static void setWaterMark (File targetFile, String rootPath, String path) throws IOException {// source File Image src = ImageIO. read (targetFile); int width = src. getWidth (null); int height = src. getHeight (null); BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB); Graphics g = image. createGraphics (); g. drawImage (src, 0, 0, width, height, null); // watermark file String FILENAME = rootPath + "ueditor/image/waterMark.png"; // when FILENAME is a url address, example: http://www.baidu.com/abc.png// URL url = new URL (FILENAME); // InputStream pressIs = url. openStream (); // when FILENAME is a local path, such as: D:/abc.png InputStream pressIs = new FileInputStream (FILENAME); Image src_biao = ImageIO. read (pressIs); int width_biao = src_biao.getWidth (null); int height_biao = src_biao.getHeight (null); g. drawImage (src_biao, width-width_biao, height-height_biao, width_biao, height_biao, null); g. dispose (); FileOutputStream out = new FileOutputStream (path); required imageencoder encoder = required codec. createJPEGEncoder (out); encoder. encode (image); out. close ();}

2) modify the saveTmpFile Method

Private static State saveTmpFile (File tmpFile, String rootPath, String path, Long maxSize) {State state State = null; File targetFile = new File (path); if (targetFile. canWrite () {return new BaseState (false, AppInfo. PERMISSION_DENIED);} try {FileUtils. moveFile (tmpFile, targetFile);} catch (IOException e) {return new BaseState (false, AppInfo. IO_ERROR);} // determine whether the image file is an if (maxSize = 2048000) {try {// Add the watermark setWaterMark (targetFile, rootPath, path);} catch (IOException e) {e. printStackTrace () ;}} state = new BaseState (true); state. putInfo ("size", targetFile. length (); state. putInfo ("title", targetFile. getName (); return state ;}

3. Restart Image Upload and then directly add a watermark.

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.