kindeditor 隐藏网络图片功能,只保留本地上传功能,并且将图片进行压缩处理
kindeditor 隐藏网络图片功能,只保留本地上传功能,并且将图片进行压缩处理
在使用kindeitor的时候,我们往往在使用图片功能的时候,包含两个部分,网络图片功能和本地上传功能,本文就告诉你如何隐 藏网络图片功能,只保存本地上传功能。
假如你想减轻服务器的,我们还可以在本地上传的时候将图片进行压缩处理,避免图片过大。本文将提供不改变原图片大小,改变(规定宽度和高度)原图片大小两种方法。
1. 隐藏网络图片功能,只保存本地上传功能
没有隐藏网络图片功能的时候,效果如下:
我们需要在如下的文件进行修改:kideditor------>plugins------>image------>images------>image.js
找到image.js,然后在第295行修改为: showRemote : false,
修改后效果下:
注意:假如达不到上图效果,一定要清除浏览器缓存!!!
以上是只保留该本地上传功能,如果您想压缩上传图片大小,我们可以进行下一步的操作!
2. 通过 图片压缩算法改变图片的大小(改变图片的宽度和高度)
第一步:找到所需修改的文件upload_json.jsp
我的文件放在如下:kindeditor----->upload_json.jsp
如下图:
第二步:修改upload_json.jsp文件的内容:(需要修改的地方自己比对吧,可以将整个代码copy,好使!!!)
3. 上述是不改变图片的大小,进行压缩处理,若想进行图片的大小设置在上述代码中删除 :165行到175行的代码(删除如下代码:)
并且将163行的代码: int newWidth; 修改为:int newWidth=img.getWidth(null);
将164行的代码: int newHeight; 修改为:int newHeight=img.getHeight(null);
2. 通过 图片压缩算法改变图片的大小(改变图片的宽度和高度)
第一步:找到所需修改的文件upload_json.jsp
我的文件放在如下:kindeditor----->upload_json.jsp
如下图:
第二步:修改upload_json.jsp文件的内容:(需要修改的地方自己比对吧,可以将整个代码copy,好使!!!)
- <%@page import="com.ccgj.platform.jjwx.imgbean"%>
- <%@page import="com.google.gson.Gson"%>
- <%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
- <%@page import="java.util.*,java.io.*" %>
- <%@page import="java.text.SimpleDateFormat" %>
- <%@page import="org.apache.commons.fileupload.*" %>
- <%@page import="org.apache.commons.fileupload.disk.*" %>
- <%@page import="org.apache.commons.fileupload.servlet.*" %>
- <%@page import="org.json.simple.*" %>
- <!-- 图片压缩引用的包 -->
- <%@page import="java.awt.Image"%>
- <%@page import="java.awt.image.BufferedImage"%>
- <%@page import="java.io.File"%>
- <%@page import="java.io.FileOutputStream"%>
- <%@page import="java.io.IOException"%>
- <%@page import="javax.imageio.ImageIO"%>
- <%@page import="com.sun.image.codec.jpeg.JPEGCodec"%>
- <%@page import="com.sun.image.codec.jpeg.JPEGImageEncoder"%>
- <%
- /**
- * KindEditor JSP
- *
- * 本JSP程序是演示程序,建议不要直接在实际项目中使用。
- * 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置。
- *
- */
- //文件保存目录路径
- String savePath = pageContext.getServletContext().getRealPath("/") + "attached/";
- //文件保存目录URL
- String saveUrl = request.getContextPath() + "/attached/";
- //定义允许上传的文件扩展名
- HashMap<String, String> extMap = new HashMap<String, String>();
- extMap.put("image", "gif,jpg,jpeg,png,bmp");
- extMap.put("flash", "swf,flv");
- extMap.put("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
- extMap.put("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");
- //最大文件大小
- //long maxSize = 2000*1024;//2MB
- response.setContentType("text/html; charset=UTF-8");
- if(!ServletFileUpload.isMultipartContent(request)){
- out.println(getError("请选择文件。"));
- return;
- }
- //检查目录
- File uploadDir = new File(savePath);
- if(!uploadDir.isDirectory()){
- out.println(getError("上传目录不存在。"));
- return;
- }
- //检查目录写权限
- if(!uploadDir.canWrite()){
- out.println(getError("上传目录没有写权限。"));
- return;
- }
- String dirName = request.getParameter("dir");
- if (dirName == null) {
- dirName = "image";
- }
- if(!extMap.containsKey(dirName)){
- out.println(getError("目录名不正确。"));
- return;
- }
- //创建文件夹
- savePath += dirName + "/";
- saveUrl += dirName + "/";
- File saveDirFile = new File(savePath);
- if (!saveDirFile.exists()) {
- saveDirFile.mkdirs();
- }
- SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
- String ymd = sdf.format(new Date());
- savePath += ymd + "/";
- saveUrl += ymd + "/";
- File dirFile = new File(savePath);
- if (!dirFile.exists()) {
- dirFile.mkdirs();
- }
- FileItemFactory factory = new DiskFileItemFactory();
- ServletFileUpload upload = new ServletFileUpload(factory);
- upload.setHeaderEncoding("UTF-8");
- List items = upload.parseRequest(request);
- Iterator itr = items.iterator();
- while (itr.hasNext()) {
- FileItem item = (FileItem) itr.next();
- String fileName = item.getName();
- long fileSize = item.getSize();
- if (!item.isFormField()) {
- /* if(item.getSize() > maxSize){
- System.out.println("文件较大");
- //out.println(getError("上传文件大小超过限制。"));
- //return;
- } */
- //检查扩展名
- String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
- if(!Arrays.<String>asList(extMap.get(dirName).split(",")).contains(fileExt)){
- out.println(getError("上传文件扩展名是不允许的扩展名。\n只允许" + extMap.get(dirName) + "格式。"));
- return;
- }
- SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
- String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
- try{
- File uploadedFile = new File(savePath, newFileName);
- item.write(uploadedFile);
- //调用压缩方法
- newFileName=compressPic(savePath, savePath, newFileName, newFileName);
- }catch(Exception e){
- out.println(getError("上传文件失败。"));
- return;
- }
- //JSONObject obj = new JSONObject();
- //obj.put("error", 0);
- //obj.put("url", saveUrl + newFileName);
- Map map = new HashMap();
- map.put("error", 0);
- map.put("url", saveUrl + newFileName);
- map.put("fileName",newFileName);
- Gson gson = new Gson();
- out.print(gson.toJson(map));
- //out.println(obj.toJSONString());
- }
- }
- %>
- <%!
- public static String compressPic(String inputDir, String outputDir, String inputFileName, String outputFileName) {
- long minLength = 200 * 1024;
- try {
- File file = new File(inputDir + inputFileName);
- Image img = ImageIO.read(file);
- int newWidth;
- int newHeight;
- //图片宽度大于858px,设置为858px
- if(img.getWidth(null)>858){
- newWidth =858;
- }
- else{newWidth=img.getWidth(null);}
- //图片高度大于500px,设置为500px
- if(img.getHeight(null)>500)
- {newHeight=500;}
- else{
- newHeight=img.getHeight(null);
- }
- // 获取源文件,如果源文件小于200KB,不做压缩处理
- if(minLength > file.length()) {
- return inputFileName;
- }
- /*
- Image img = ImageIO.read(file);
- int newWidth = img.getWidth(null);
- System.out.println("width"+newWidth);
- int newHeight = img.getHeight(null); */
- BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);
- tag.getGraphics().drawImage(
- img.getScaledInstance(newWidth, newHeight,
- Image.SCALE_SMOOTH), 0, 0, null);
- FileOutputStream out = new FileOutputStream(outputDir + outputFileName);
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- encoder.encode(tag);
- out.close();
- // 压缩完成后,删除源文件
- // file.delete();
- } catch (IOException ex) {
- ex.printStackTrace();
- return inputFileName;
- }
- return outputFileName;
- }
- %>
- <%!
- private String getError(String message) {
- JSONObject obj = new JSONObject();
- obj.put("error", 1);
- obj.put("message", message);
- return obj.toJSONString();
- }
- %>
3. 上述是不改变图片的大小,进行压缩处理,若想进行图片的大小设置在上述代码中删除 :165行到175行的代码(删除如下代码:)
- //图片宽度大于858px,设置为858px
- if(img.getWidth(null)>858){
- newWidth =858;
- }
- else{newWidth=img.getWidth(null);}
- //图片高度大于500px,设置为500px
- if(img.getHeight(null)>500)
- {newHeight=500;}
- else{
- newHeight=img.getHeight(null);
- }
- // 获取源文件,如果源文件小于200KB,不做压缩处理
- if(minLength > file.length()) {
- return inputFileName;
- }
并且将163行的代码: int newWidth; 修改为:int newWidth=img.getWidth(null);
将164行的代码: int newHeight; 修改为:int newHeight=img.getHeight(null);