项目中采用的是kindeditor3.4
UploadContentImgAction
@SuppressWarnings("serial")
@ParentPackage("control-center")
public class UploadContentImgAction extends BaseAction {
private File up;
private String upFileName;
private String upContentType;
private String fileDir = "uploads/articleContentImg";
private String imgTitle;
private String align;
private int imgWidth;
private int imgHeight;
/**
* kindeditor图片上传
* @return
* @throws Exception
*/
@Action("kindeditorImgUpload")
public String kindeditorImgUpload() throws Exception {
//只能传图片
try {
if(!validatePostfix(upFileName)) {
return "error";
}
User user = (User)getSession().get("user");
String fileRealDir = getServletContext().getRealPath(fileDir);
File file = up;
String fileRealName = createfilename(user.getUserId());
String fileName = fileRealName + upFileName.substring(upFileName.lastIndexOf(".")).toLowerCase();
File newFile = new File(fileRealDir, fileName);
FileUtils.copyFile(file, newFile);
//压缩图片
ImageUtil.resize(newFile.getPath(), newFile.getPath(), 500);
String id = "contentId";
String url = "/" + fileDir + "/" + fileName;
String border = "0";
String result = "<script type='text/javascript'>parent.KE.plugin['image'].insert('" + id + "','" + url + "','" + imgTitle + "','" + imgHeight + "','" + imgHeight + "','" + border + "','" + align + "');</script>";
getHttpServletResponse().getWriter().write(result);
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 生成文件名 : 当前时间 + 随机数 + 用户id
*/
private String createfilename(int userId) {
StringBuilder result = new StringBuilder();
// 得到 本地的 当前时间
String now = DateUtil.getLongStrFromDate(new Date());
// 在 1000W 内随机生成一个数字
int rand = new Random().nextInt(9999999);
// 去掉 - 去掉 : 去掉 空格 后,返回
result.append(now.replace("-", "").replace(":", "").replace(" ", "")).append("_").append(rand).append("_").append(userId);
return result.toString();
}
/**
* 验证后缀名 应该从配置文件中获取
*/
public boolean validatePostfix(String filename) {
// 定义可上传文件的 类型
List<String> fileTypes = new ArrayList<String>();
// 图片
fileTypes.add("jpg");
fileTypes.add("jpeg");
fileTypes.add("bmp");
fileTypes.add("gif");
fileTypes.add("png");
// 得到文件尾数 并 进行小写转换
String postfix = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
return fileTypes.contains(postfix) ? true : false;
}
public File getUp() {
return up;
}
public void setUp(File up) {
this.up = up;
}
public String getUpFileName() {
return upFileName;
}
public void setUpFileName(String upFileName) {
this.upFileName = upFileName;
}
public String getUpContentType() {
return upContentType;
}
public void setUpContentType(String upContentType) {
this.upContentType = upContentType;
}
public String getImgTitle() {
return imgTitle;
}
public void setImgTitle(String imgTitle) {
this.imgTitle = imgTitle;
}
public int getImgWidth() {
return imgWidth;
}
public void setImgWidth(int imgWidth) {
this.imgWidth = imgWidth;
}
public int getImgHeight() {
return imgHeight;
}
public void setImgHeight(int imgHeight) {
this.imgHeight = imgHeight;
}
public String getAlign() {
return align;
}
public void setAlign(String align) {
this.align = align;
}
}
ImageUitl
package com.yancheng.myframe.util;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import magick.ImageInfo;
import magick.MagickImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* @author
*
*/
public class ImageUtil {
public final static int PHOTO_RATIO = 800; //缩放图片系数
static{
System.setProperty("jmagick.systemclassloader", "no");
}
/**
* 图片水印
*
* @param pressImg 水印图片
* @param targetImg 目标图片
* @param x 修正值 默认在中间
* @param y 修正值 默认在中间
* @param alpha 透明度
*/
public final static void pressImage(String pressImg, String targetImg,
int x, int y, float alpha) {
try {
File img = new File(targetImg);
Image src = ImageIO.read(img);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
// 水印文件
Image src_biao = ImageIO.read(new File(pressImg));
int wideth_biao = src_biao.getWidth(null);
int height_biao = src_biao.getHeight(null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
g.drawImage(src_biao, (wideth - wideth_biao) / 2,
(height - height_biao) / 2, wideth_biao, height_biao, null);
// 水印文件结束
g.dispose();
ImageIO.write((BufferedImage) image, "jpg", img);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文字水印
*
* @param pressText 水印文字
* @param targetImg 目标图片
* @param fontName 字体名称
* @param fontStyle 字体样式
* @param color 字体颜色
* @param fontSize 字体大小
* @param x 修正值
* @param y 修正值
* @param alpha 透明度
*/
public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color, int fontSize, int x,
int y, float alpha) {
try {
File img = new File(targetImg);
Image src = ImageIO.read(img);
int width = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage(src, 0, 0, width, height, null);
g.setColor(color);
g.setFont(new Font(fontName, fontStyle, fontSize));
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
g.drawString(pressText, (width - (getLength(pressText) * fontSize)) / 2 + x, (height - fontSize) / 2 + y);
g.dispose();
ImageIO.write((BufferedImage) image, "jpg", img);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 缩放 效果太差 ps: Graphics下的 还有 AffineTransform下的
* 缩放都是针对"图形"而不是"图像"的,所以处理后图片很不清晰
* @param filePath 图片路径
* @param height 高度
* @param width 宽度
* @param bb 比例不对时是否需要补白
*/
public static void resizeImgcale(String filePath, int height, int width, boolean bb) {
try {
double ratio = 0.0; // 缩放比例
File f = new File(filePath);
BufferedImage bi = ImageIO.read(f);
Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
// 计算比例
if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
if (bi.getHeight() > bi.getWidth()) {
ratio = (new Integer(height)).doubleValue() / bi.getHeight();
} else {
ratio = (new Integer(width)).doubleValue() / bi.getWidth();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
itemp = op.filter(bi, null);
}
if (bb) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
if (width == itemp.getWidth(null))
g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,
itemp.getWidth(null), itemp.getHeight(null),
Color.white, null);
else
g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,
itemp.getWidth(null), itemp.getHeight(null),
Color.white, null);
g.dispose();
itemp = image;
}
ImageIO.write((BufferedImage) itemp, "jpg", f);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 计算字的长度
*
* @param text
* @return
*/
public static int getLength(String text) {
int length = 0;
for (int i = 0; i < text.length(); i++) {
if (new String(text.charAt(i) + "").getBytes().length > 1) {
length += 2;
} else {
length += 1;
}
}
return length / 2;
}
/**
* 压缩图片
*
* @param imgsrc 源文件
* @param imgdist 目标文件
* @param widthdist 宽
* @param heightdist 高
*/
public static void resizeImg(String imgsrc, String imgdist, int widthdist, int heightdist) {
try {
File srcfile = new File(imgsrc);
if (!srcfile.exists()) {
return;
}
Image src = javax.imageio.ImageIO.read(srcfile);
BufferedImage tag = new BufferedImage(widthdist, heightdist, BufferedImage.TYPE_INT_RGB);
/*
* SCALE_SMOOTH:尺寸平滑 SCALE_AREA_AVERAGING:尺度区平均 SCALE_FAST:尺度快速
* SCALE_REPLICATE:尺度复制
*/
tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist, Image.SCALE_SMOOTH), 0, 0, null);
FileOutputStream out = new FileOutputStream(imgdist);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* 图片压缩
* @param picFrom
* @param picTo
* @param widthdist
* @param heightdist
*/
public static void resize(String picFrom, String picTo, int widthdist, int heightdist) {
try {
ImageInfo info = new ImageInfo(picFrom);
MagickImage image = new MagickImage(new ImageInfo(picFrom));
MagickImage scaled = image.scaleImage(widthdist, heightdist);// 小图片文件的大小.
scaled.setFileName(picTo);
scaled.writeImage(info);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void resize(String picFrom, String picTo, int ratio) throws Exception {
BufferedImage bi = ImageIO.read(new File(picFrom));
//原始图片属性
int srcWidth = bi.getWidth();
int srcHeight = bi.getHeight();
//生成图片属性
int newWidth = srcWidth;
int newHeight = srcHeight;
//如果超出最大宽或高就压缩
if (srcWidth > ratio || newHeight > ratio) {
//生成图片width, height计算
if (srcWidth >= srcHeight) {
if (srcWidth < ratio) {
return;
}
newWidth = ratio;
newHeight = (int)(ratio * srcHeight / srcWidth);
} else {
if (srcHeight < ratio) {
return;
}
newHeight = ratio;
newWidth = (int)(ratio * srcWidth / srcHeight);
}
}
resize(picFrom, picTo, newWidth, newHeight);
}
public static void resize(String picFrom, String picTo) throws Exception {
resize(picFrom, picTo, PHOTO_RATIO);
}
public static void main(String[] args) throws Exception {
// resizeImg("d:/411766.jpg", "d:/411766_1.jpg", 800, 600);
// resize("d:/test_4.jpg", "d:/test_4_2.jpg", 800);
pressText("欢喜冤家", "d:/411766.jpg", "简体", Font.ITALIC, Color.black, 90, 40, 80, 0.5f);
}
}
分享到:
相关推荐
kindeditor上传图片时自动将过大(像素以及图片长宽)的图片进行压缩,只需要在kindeditor中upload_json.ashx里面修改!
vue 使用kindeditor粘贴复制图片上传给自己服务器,单独图片上传,避免用户到处寻找,解决了用户粘贴复制截图时候,图片变成base64,上传给后台的时候长度过于长,所以在粘贴的时候就上传给服务器,最后以形式显示在富...
2、复制struts需要的jar包和kindEditor必须jar包到lib下,因为它们的jar包邮重复,选择最新的就ok 3、如果需要后台java代码获取内容,可以在action中定义content属性,getContent就可以获取到 需要注意的是,这里的 ...
kindeditor本地上传图片支持token字段,kindeditor本身图片上传并无token参数,本资源利用原生ajax改造上传代码。
介绍kindeditor环境部署已经如何进行图片上传。整合struts2以及页面bootstrap展现来进行 详细介绍。kindeditor版本为4.1.10
.NET MVC 中使用 kindeditor 中图片上传和查看功能 的正确使用方式
struts2image kindeditor
这是本人写的一个基于KindEditor-4.1.9的Java项目,采用Struts2框架,实现了KindEditor单张图片上传、批量图片上传,可以上传到image目录下的子目录中,可返回上一级目录,并实现了按名称、大小、类型排序的功能。...
kindeditor整合struts2 增加删除图片功能
kindeditor添加图片压缩功能asp.net版本,从网上找了好多都找不到,自己做了下
kindeditor(带压缩图片功能),网上找了好久都没有找到,这个是在一个小网站中收到的。。
kindeditor4.1.4 图片压缩,非常好用,亲测能用。网站只有ASP.net版本的,自己加了ASP版,内带说明文件。
kindEditor4.1版修改上传图片宽高(图片自适应)
KindEditor上传图片和管理图片 管理图片采用直接读取磁盘文件模式 若在现实中使用则应该使用数据库模式
只需把这个 upload_json.jsp 文件替换 原有的即可 轻松加愉快
对kindeditor富文本编辑器做了修改,使其适用于struts2框架 解决了javascript动态生成的textarea无法显示工具栏的问题. 内附使用说明,操作很简单的。
该实例解决了kindeditor编辑器与struts2"上传错误"的问题,能够直接运行!
今天本人才接触KindEditor,一看得到这个外观漂亮,也是JS写的,觉得比较好用就开始琢磨自己来拿用 但是在用时就发现很多问题! 其中图片上传就是一个 所以就自己写了个Demo研究,现在总算没问题了 这个Demo希望...
优化kindeditor,修改flash批量上传图片方式为H5上传,添加第三方视频代码功能,添加手机尺寸预览,粘贴时自动上传网络图片,粘贴时自动上传截图,地图宽度可设置百分比显示...
修复kindeditor官方对集成了Struts2框架上传文件无法上传的问题!