kindeditor 隐藏网络图片功能,只保留本地上传功能,并且将图片进行压缩处理

kindeditor 隐藏网络图片功能,只保留本地上传功能,并且将图片进行压缩处理

       在使用kindeitor的时候,我们往往在使用图片功能的时候,包含两个部分,网络图片功能和本地上传功能,本文就告诉你如何隐  藏网络图片功能,只保存本地上传功能。

    假如你想减轻服务器的,我们还可以在本地上传的时候将图片进行压缩处理,避免图片过大。本文将提供不改变原图片大小,改变(规定宽度和高度)原图片大小两种方法。

1.    隐藏网络图片功能,只保存本地上传功能

       没有隐藏网络图片功能的时候,效果如下:

                                                                                      kindeditor 隐藏网络图片功能,只保留本地上传功能,并且将图片进行压缩处理

        我们需要在如下的文件进行修改:kideditor------>plugins------>image------>images------>image.js

      kindeditor 隐藏网络图片功能,只保留本地上传功能,并且将图片进行压缩处理

                        找到image.js,然后在第295行修改为:    showRemote : false,

                                                    kindeditor 隐藏网络图片功能,只保留本地上传功能,并且将图片进行压缩处理

                       修改后效果下:

                                                                  kindeditor 隐藏网络图片功能,只保留本地上传功能,并且将图片进行压缩处理

                     注意:假如达不到上图效果,一定要清除浏览器缓存!!!


          以上是只保留该本地上传功能,如果您想压缩上传图片大小,我们可以进行下一步的操作!

2.    通过 图片压缩算法改变图片的大小(改变图片的宽度和高度)
    
       第一步:找到所需修改的文件upload_json.jsp

                        我的文件放在如下:kindeditor----->upload_json.jsp

                          如下图:
                                                             kindeditor 隐藏网络图片功能,只保留本地上传功能,并且将图片进行压缩处理
         第二步:修改upload_json.jsp文件的内容:(需要修改的地方自己比对吧,可以将整个代码copy,好使!!!)
        
      
  1. <%@page import="com.ccgj.platform.jjwx.imgbean"%>  
  2. <%@page import="com.google.gson.Gson"%>  
  3. <%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  4. <%@page import="java.util.*,java.io.*" %>  
  5. <%@page import="java.text.SimpleDateFormat" %>  
  6. <%@page import="org.apache.commons.fileupload.*" %>  
  7. <%@page import="org.apache.commons.fileupload.disk.*" %>  
  8. <%@page import="org.apache.commons.fileupload.servlet.*" %>  
  9. <%@page import="org.json.simple.*" %>  
  10.   
  11. <!-- 图片压缩引用的包 -->  
  12. <%@page import="java.awt.Image"%>  
  13. <%@page import="java.awt.image.BufferedImage"%>  
  14. <%@page import="java.io.File"%>  
  15. <%@page import="java.io.FileOutputStream"%>  
  16. <%@page import="java.io.IOException"%>  
  17. <%@page import="javax.imageio.ImageIO"%>  
  18. <%@page import="com.sun.image.codec.jpeg.JPEGCodec"%>  
  19. <%@page import="com.sun.image.codec.jpeg.JPEGImageEncoder"%>  
  20.    
  21.   
  22. <%  
  23.   
  24.   
  25. /** 
  26.  * KindEditor JSP 
  27.  *  
  28.  * 本JSP程序是演示程序,建议不要直接在实际项目中使用。 
  29.  * 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置。 
  30.  *  
  31.  */  
  32.   
  33. //文件保存目录路径  
  34. String savePath = pageContext.getServletContext().getRealPath("/") + "attached/";  
  35. //文件保存目录URL  
  36. String saveUrl  = request.getContextPath() + "/attached/";  
  37.   
  38.   
  39.   
  40. //定义允许上传的文件扩展名  
  41. HashMap<String, String> extMap = new HashMap<String, String>();  
  42. extMap.put("image""gif,jpg,jpeg,png,bmp");  
  43. extMap.put("flash""swf,flv");  
  44. extMap.put("media""swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");  
  45. extMap.put("file""doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");  
  46.   
  47. //最大文件大小  
  48. //long maxSize = 2000*1024;//2MB  
  49.    
  50.   
  51. response.setContentType("text/html; charset=UTF-8");  
  52.   
  53. if(!ServletFileUpload.isMultipartContent(request)){  
  54.     out.println(getError("请选择文件。"));  
  55.     return;  
  56. }  
  57. //检查目录  
  58. File uploadDir = new File(savePath);  
  59. if(!uploadDir.isDirectory()){  
  60.     out.println(getError("上传目录不存在。"));  
  61.     return;  
  62. }  
  63. //检查目录写权限  
  64. if(!uploadDir.canWrite()){  
  65.     out.println(getError("上传目录没有写权限。"));  
  66.     return;  
  67. }  
  68.   
  69. String dirName = request.getParameter("dir");  
  70. if (dirName == null) {  
  71.     dirName = "image";  
  72. }  
  73. if(!extMap.containsKey(dirName)){  
  74.     out.println(getError("目录名不正确。"));  
  75.     return;  
  76. }  
  77. //创建文件夹  
  78. savePath += dirName + "/";  
  79. saveUrl += dirName + "/";  
  80. File saveDirFile = new File(savePath);  
  81. if (!saveDirFile.exists()) {  
  82.     saveDirFile.mkdirs();  
  83. }  
  84. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");  
  85. String ymd = sdf.format(new Date());  
  86. savePath += ymd + "/";  
  87. saveUrl += ymd + "/";  
  88. File dirFile = new File(savePath);  
  89. if (!dirFile.exists()) {  
  90.     dirFile.mkdirs();  
  91. }  
  92.   
  93. FileItemFactory factory = new DiskFileItemFactory();  
  94. ServletFileUpload upload = new ServletFileUpload(factory);  
  95. upload.setHeaderEncoding("UTF-8");  
  96. List items = upload.parseRequest(request);  
  97. Iterator itr = items.iterator();  
  98.   
  99.     
  100.    
  101.   
  102. while (itr.hasNext()) {  
  103.     FileItem item = (FileItem) itr.next();  
  104.     String fileName = item.getName();  
  105.     long fileSize = item.getSize();  
  106.       
  107.     if (!item.isFormField()) {  
  108.          
  109.     /*  if(item.getSize() > maxSize){ 
  110.            System.out.println("文件较大"); 
  111.             //out.println(getError("上传文件大小超过限制。")); 
  112.             //return; 
  113.         } */  
  114.           
  115.         //检查扩展名  
  116.         String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();  
  117.         if(!Arrays.<String>asList(extMap.get(dirName).split(",")).contains(fileExt)){  
  118.             out.println(getError("上传文件扩展名是不允许的扩展名。\n只允许" + extMap.get(dirName) + "格式。"));  
  119.             return;  
  120.         }  
  121.   
  122.         SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");  
  123.         String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;  
  124.         try{  
  125.             File uploadedFile = new File(savePath, newFileName);  
  126.             item.write(uploadedFile);  
  127.         //调用压缩方法  
  128.             newFileName=compressPic(savePath, savePath, newFileName, newFileName);  
  129.               
  130.         }catch(Exception e){  
  131.             out.println(getError("上传文件失败。"));  
  132.             return;  
  133.         }  
  134.   
  135.         //JSONObject obj = new JSONObject();  
  136.         //obj.put("error", 0);  
  137.         //obj.put("url", saveUrl + newFileName);  
  138.           
  139.         Map map = new HashMap();  
  140.         map.put("error"0);  
  141.         map.put("url", saveUrl + newFileName);  
  142.           
  143.         map.put("fileName",newFileName);  
  144.           
  145.           
  146.         Gson gson = new Gson();  
  147.           
  148.         out.print(gson.toJson(map));  
  149.           
  150.         //out.println(obj.toJSONString());  
  151.   
  152.     }  
  153. }  
  154. %>  
  155.   
  156.   
  157. <%!  
  158.    public static String compressPic(String inputDir, String outputDir, String inputFileName, String outputFileName) {      
  159.             long minLength = 200 * 1024;    
  160.          try {      
  161.             File file = new File(inputDir + inputFileName);   
  162.             Image img = ImageIO.read(file);  
  163.             int newWidth;  
  164.             int newHeight;  
  165.             //图片宽度大于858px,设置为858px  
  166.             if(img.getWidth(null)>858){  
  167.               newWidth =858;  
  168.             }    
  169.             else{newWidth=img.getWidth(null);}  
  170.             //图片高度大于500px,设置为500px  
  171.             if(img.getHeight(null)>500)  
  172.               {newHeight=500;}  
  173.               else{  
  174.                 newHeight=img.getHeight(null);             
  175.               }  
  176.            
  177.             // 获取源文件,如果源文件小于200KB,不做压缩处理    
  178.               
  179.             if(minLength > file.length()) {    
  180.                 return inputFileName;    
  181.             }    
  182.                 
  183.           /*    
  184.           Image img = ImageIO.read(file);   
  185.  
  186.             int newWidth = img.getWidth(null);   
  187.            System.out.println("width"+newWidth); 
  188.             int newHeight = img.getHeight(null);  */   
  189.              
  190.             BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);    
  191.             tag.getGraphics().drawImage(    
  192.                     img.getScaledInstance(newWidth, newHeight,    
  193.                             Image.SCALE_SMOOTH), 00null);    
  194.             FileOutputStream out = new FileOutputStream(outputDir + outputFileName);    
  195.             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);    
  196.             encoder.encode(tag);    
  197.             out.close();    
  198.                 
  199.             // 压缩完成后,删除源文件    
  200.            // file.delete();    
  201.                 
  202.         } catch (IOException ex) {    
  203.             ex.printStackTrace();    
  204.             return inputFileName;    
  205.         }    
  206.         return outputFileName;    
  207.     }    
  208. %>  
  209.   
  210. <%!  
  211. private String getError(String message) {  
  212.     JSONObject obj = new JSONObject();  
  213.     obj.put("error"1);  
  214.     obj.put("message", message);  
  215.     return obj.toJSONString();  
  216. }  
  217. %>  




                       
   3. 上述是不改变图片的大小,进行压缩处理,若想进行图片的大小设置在上述代码中删除  :165行到175行的代码(删除如下代码:)
      
          
  1. //图片宽度大于858px,设置为858px  
  2.           if(img.getWidth(null)>858){  
  3.             newWidth =858;  
  4.           }    
  5.           else{newWidth=img.getWidth(null);}  
  6.           //图片高度大于500px,设置为500px  
  7.           if(img.getHeight(null)>500)  
  8.             {newHeight=500;}  
  9.             else{  
  10.                  newHeight=img.getHeight(null);             
  11.             }  
  12.          
  13.           // 获取源文件,如果源文件小于200KB,不做压缩处理    
  14.             
  15.           if(minLength > file.length()) {    
  16.               return inputFileName;    
  17.           }  

 

      并且将163行的代码: int newWidth;   修改为:int newWidth=img.getWidth(null);
              将164行的代码: int newHeight;  修改为:int newHeight=img.getHeight(null);