本文主要为大家详细介绍了利用imgareaselect辅助后台实现图片裁剪的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能帮助到大家。
因为项目当中用到图片裁剪,本来可以不用到后台进行裁剪的,但是要兼容万恶的IE浏览器,所以不得不使用后台进行裁剪。
这次使用到imgareaselect 插件获取需要裁剪区域的坐标,再由后台进行裁剪操作。先上个效果图再说
但是这里有一个坑就是上传的图片过大,可能会造成裁剪的区域跟插件中显示的不一样,所以得现在后台对云图片进行压缩在裁剪。
/** * 等比例压缩算法: * 算法思想:根据压缩基数和压缩比来压缩原图,生产一张图片效果最接近原图的缩略图 * @param srcURL 原图地址 * @param deskURL 缩略图地址 * @param comBase 压缩基数 * @param scale 压缩限制(宽/高)比例 一般用1: * 当scale>=1,缩略图height=comBase,width按原图宽高比例;若scale comBase || (double) srcWidth > comBase) { if (srcScale >= scale || 1 / srcScale > scale) { if (srcScale >= scale) { deskHeight = (int) comBase; deskWidth = srcWidth * deskHeight / srcHeight; } else { deskWidth = (int) comBase; deskHeight = srcHeight * deskWidth / srcWidth; } } else { if ((double) srcHeight > comBase) { deskHeight = (int) comBase; deskWidth = srcWidth * deskHeight / srcHeight; } else { deskWidth = (int) comBase; deskHeight = srcHeight * deskWidth / srcWidth; } } } else { deskHeight = srcHeight; deskWidth = srcWidth; } BufferedImage tag = new BufferedImage(deskWidth, deskHeight, BufferedImage.TYPE_3BYTE_BGR); tag.getGraphics().drawImage(src, 0, 0, deskWidth, deskHeight, null); //绘制缩小后的图 FileOutputStream deskImage = new FileOutputStream(deskURL); //输出到文件流 ImageIO.write(tag, ext, new File(deskURL)); deskImage.close(); }
登录后复制
这就是压缩之后在进行裁剪了,好了上完整代码先
nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">上传头像
上传头像
选择图片
登录后复制
js 代码
function preview(img, selection) { if (!selection.width || !selection.height) return; var scaleX = 100 / selection.width; var scaleY = 100 / selection.height; $('#preview img').css({ width: Math.round(scaleX * 300), height: Math.round(scaleY * 300), marginLeft: -Math.round(scaleX * selection.x1), marginTop: -Math.round(scaleY * selection.y1) }); $('#x1').val(selection.x1); $('#y1').val(selection.y1); $('#x2').val(selection.x2); $('#y2').val(selection.y2); $('#w').val(selection.width); $('#h').val(selection.height); } $(function () { }); //上传原始图片 function uplodImage(target) { if(checkImage(target)){ var url = httpUtils.rootPath+'/component/upload.do'; $.ajaxFileUpload({ url: url, //用于文件上传的服务器端请求地址 secureuri: false, //是否需要安全协议,一般设置为false fileElementId: 'uplodimage', //文件上传域的ID dataType: 'json', //返回值类型 一般设置为json success: function (data) //服务器成功响应处理函数 { var filePath = data.filePath; $('#photo').attr('src',httpUtils.rootPath+'/component/upload.do?action=download&filepath='+filePath); $('#smallImage').attr('src',httpUtils.rootPath+'/component/upload.do?action=download&filepath='+filePath); $('#imgUrl').val(filePath); $('#photo').imgAreaSelect({ aspectRatio: '1:1', x1: 50, y1: 50, x2: 241, y2: 241, handles: true, fadeSpeed: 200, onSelectChange: preview }); $('#x1').val("50"); $('#y1').val("50"); $('#x2').val("241"); $('#y2').val("241"); $('#w').val("191"); $('#h').val("191"); } }); } } //上传裁剪后的图片 function upold() { if($('#imgUrl').val()==''){ layer.alert("请先选择图片在上传"); return false; } $.ajax({ type: "post", url:httpUtils.rootPath+'/user/setHeadPicture', beforeSend: function(request) { request.setRequestHeader("jmtcp", header); }, async:false, data: { x:$('#x1').val(), y:$('#y1').val(), width: $('#w').val(), imgUrl : $('#imgUrl').val(), heigth: $('#h').val() }, dataType: "json", success: function(data){ if(data.code==1){ layer.alert(data.message,function(){ window.location.href = '../user/grzy.html'; }); }else{ layer.alert(data.message); } } }); } function checkImage(target) { var isIE = /msie/i.test(navigator.userAgent) && !window.opera; var fileSize = 0; var filepath = target.value; // 为了避免转义反斜杠出问题,这里将对其进行转换 var re = /(+)/g; var filename = filepath.replace(re, "#"); // 对路径字符串进行剪切截取 var one = filename.split("#"); // 获取数组中最后一个,即文件名 var two = one[one.length - 1]; // 再对文件名进行截取,以取得后缀名 var three = two.split("."); // 获取截取的最后一个字符串,即为后缀名 var last = three[three.length - 1]; // 添加需要判断的后缀名类型 var tp = "jpg,gif,bmp,JPG,GIF,BMP,png"; // 返回符合条件的后缀名在字符串中的位置 var rs = tp.indexOf(last); // 如果返回的结果大于或等于0,说明包含允许上传的文件类型 if(rs 5) { layer.alert("图片不能大于5M"); return false; } return true; }
登录后复制
后台代码
public class CutImageUtils { public static SecureRandom rnd = new SecureRandom(); public static String cutImage(int x, int y, int width, int height,String srcPath) throws Exception{ String root = ApplicationContext.getProperty("upload_folder"); String imagePath = root+srcPath; FileInputStream is = null; ImageInputStream iis = null; String uploadFolder = null ; String filepath = null ; String serverName = null ; uploadFolder = ApplicationContext.getProperties().getProperty("upload_folder").toString() ; filepath = generateServerFolder() ; serverName = generateServerName(srcPath) ; File file = new File(uploadFolder + filepath); if (!file.exists()) { file.mkdirs(); } try { // 读取图片文件 saveMinPhoto(imagePath, imagePath, 300, 0.9d); //resetsize(imagePath, imagePath); is = new FileInputStream(imagePath); String ext = srcPath.substring(srcPath.lastIndexOf(".") + 1); Iterator it = ImageIO.getImageReadersByFormatName(ext); ImageReader reader = it.next(); // 获取图片流 iis = ImageIO.createImageInputStream(is); reader.setInput(iis, true); ImageReadParam param = reader.getDefaultReadParam(); /** * * 图片裁剪区域。Rectangle 指定了坐标空间中的一个区域,通过 Rectangle 对象 * * 的左上顶点的坐标(x,y)、宽度和高度可以定义这个区域。 */ Rectangle rect = new Rectangle(x, y, width, height); // 提供一个 BufferedImage,将其用作解码像素数据的目标。 param.setSourceRegion(rect); BufferedImage bi = reader.read(0, param); // 保存新图片 ImageIO.write(bi, ext, new File(uploadFolder + filepath + serverName)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block if (is != null) is.close(); if (iis != null) iis.close(); e.printStackTrace(); } return filepath + serverName ; } /** * * @param config * @param file * @param request * @return */ public static String generateServerName(String clientPath) { //按当前时间的分钟毫秒+3位随机数 Calendar c = Calendar.getInstance(); String min = get(c,Calendar.MINUTE); String sec = get(c,Calendar.SECOND); String mis = get(c,Calendar.MILLISECOND); String rnd = random(); String ext = getFileExt(getClientName(clientPath)); return min + sec + mis + rnd + ext ; } /** 客户端文件名 */ public static String getClientName(String clientPath) { if(null != clientPath){ int index1 = clientPath.lastIndexOf("/"); int index2 = clientPath.lastIndexOf("\"); if(index2 > index1){ index1 = index2; } return clientPath.substring(index1+1,clientPath.length()); } return null; } public static String getFileExt(String fileName){ if(null != fileName){ int dot = fileName.lastIndexOf("."); if(dot >= 0){ return fileName.substring(dot); } } return ""; } public static String random(){ String value = String.valueOf(rnd.nextInt(1000)); if(value.length() =1,缩略图height=comBase,width按原图宽高比例;若scale comBase || (double) srcWidth > comBase) { if (srcScale >= scale || 1 / srcScale > scale) { if (srcScale >= scale) { deskHeight = (int) comBase; deskWidth = srcWidth * deskHeight / srcHeight; } else { deskWidth = (int) comBase; deskHeight = srcHeight * deskWidth / srcWidth; } } else { if ((double) srcHeight > comBase) { deskHeight = (int) comBase; deskWidth = srcWidth * deskHeight / srcHeight; } else { deskWidth = (int) comBase; deskHeight = srcHeight * deskWidth / srcWidth; } } } else { deskHeight = srcHeight; deskWidth = srcWidth; } BufferedImage tag = new BufferedImage(deskWidth, deskHeight, BufferedImage.TYPE_3BYTE_BGR); tag.getGraphics().drawImage(src, 0, 0, deskWidth, deskHeight, null); //绘制缩小后的图 FileOutputStream deskImage = new FileOutputStream(deskURL); //输出到文件流 ImageIO.write(tag, ext, new File(deskURL)); deskImage.close(); } public static void main(String[] args) { try { String src = CutImageUtils.cutImage(11, 12, 100, 100, "2017/01/04/17/6348162d-5b50-4e7d-b414-93140498f8de.jpg"); System.out.println(src); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
登录后复制
相关推荐:
jquery实现自定义图片裁剪功能代码分享
关于CSS3多重背景及背景图片裁剪和定位以及尺寸的具体详解
JavaScript图片裁剪的无变形的示例代码分享(图)
以上就是imgareaselect辅助后台实现图片上传裁剪方法教程的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2789337.html