public static String uploadImg(MultipartFile file, int width, int height, boolean whFixed, boolean borderRadius, boolean needCompress, long maxSize) throws SvcSuspendedException { PropertiesInjectUtil propertiesInjectUtil = SpringContextHolder .getBean("propertiesInjectUtil"); String fileName = file.getOriginalFilename(); int suffixIndex = fileName.lastIndexOf('.'); if (suffixIndex == -1) { throw new SvcSuspendedException("上传失败!文件没有后缀名,不允许上传!"); } String name = fileName.substring(0, suffixIndex); if (StringUtils.isBlank(name)) { throw new SvcSuspendedException("上传失败!该文件名没有文件名,不允许上传!"); } String suffix = file.getContentType(); if (!UploadUtil.acceptImg.contains(suffix)) { throw new SvcSuspendedException("请上传正确的图片格式,目前仅支持jpg格式"); } if (file.getSize() > maxSize * 1000) { throw new SvcSuspendedException("上传图片不能大于" + maxSize + "K"); } try { BufferedImage srcBufferImage = ImageIO.read(file.getInputStream()); if (whFixed) { if (srcBufferImage.getWidth() != width || srcBufferImage.getHeight() != height) { throw new SvcSuspendedException("图片长度必须为" + width + "像素,高度必须为" + height + "个像素"); } } InputStream is = null; if (borderRadius) { if (!BORDER_RADIUS_ACCEPT_IMG_TYPE.contains(suffix)) { throw new SvcSuspendedException("仅PNG格式图片支持圆角"); } BufferedImage rounded = makeRoundedCorner(srcBufferImage, 30); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(rounded, "png", baos); is = new ByteArrayInputStream(baos.toByteArray()); } else { is = file.getInputStream(); } String filePath = propertiesInjectUtil.getUpload_img_url(); String path = UploadUtil.upload(is, fileName, filePath, RequestUtils.getWebpath() + "/upload/images"); if (needCompress) { String dest = UploadUtil.generateLocalRandomPath(fileName, filePath); ImageUtil.compressImg(80, path, dest); String url = UploadUtil.convertLocalPathToUrl(dest, RequestUtils.getWebpath() + "/upload/images"); return url; } return path; } catch (IOException e) { log.error("Errors when read or update.", e); throw new SvcSuspendedException("上传图片失败!"); } catch (MagickException e) { log.error("Errors when compress images.", e); throw new SvcSuspendedException("图片压缩失败!"); } }
makeRoundedCorner方法:
public static BufferedImage makeRoundedCorner(BufferedImage image, int cornerRadius) { int w = image.getWidth(); int h = image.getHeight(); BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR); Graphics2D g2 = (Graphics2D) output.getGraphics(); // g2.setBackground(Color.WHITE); // g2.clearRect(0, 0, w, h); // This is what we want, but it only does hard-clipping, i.e. aliasing // g2.setClip(new RoundRectangle2D ...) // so instead fake soft-clipping by first drawing the desired clip shape // in fully opaque white with antialiasing enabled... g2.setComposite(AlphaComposite.Src); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(Color.WHITE); Shape s = new RoundRectangle2D.Float(0, 0, w, h, cornerRadius, cornerRadius); g2.fill(s); // ... then compositing the image on top, // using the white shape from above as alpha source g2.setComposite(AlphaComposite.SrcAtop); g2.drawImage(image, 0, 0, null); g2.setComposite(AlphaComposite.Src); g2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_DEFAULT); g2.setBackground(Color.WHITE); g2.setColor(Color.WHITE); for (int x = 0; x <= w; x++) { for (int y = 0; y <= h; y++) { if (!s.contains(x, y)) { if (x < w / 2) { if (y < h / 2) { g2.drawRect(x, y, 1, 1); } else { g2.drawRect(x, y - 1, 1, 1); } } else { if (y < h / 2) { g2.drawRect(x - 1, y, 1, 1); } else { g2.drawRect(x - 1, y - 1, 1, 1); } } } } } g2.dispose(); return output; }