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.