|
@@ -0,0 +1,102 @@
|
|
|
+/*
|
|
|
+ * *************************************************
|
|
|
+ * Copyright (c) 2018 QMTH. All Rights Reserved.
|
|
|
+ * Created by Deason on 2018-06-19 14:24:38.
|
|
|
+ * *************************************************
|
|
|
+ */
|
|
|
+
|
|
|
+package cn.com.qmth.examcloud.core.questions.base;
|
|
|
+
|
|
|
+import org.jsoup.Jsoup;
|
|
|
+import org.jsoup.nodes.Document;
|
|
|
+import org.jsoup.nodes.Element;
|
|
|
+import org.jsoup.select.Elements;
|
|
|
+
|
|
|
+public class ImageUtils {
|
|
|
+ private static final String TAG_IMG = "img";
|
|
|
+ private static final String UNIT = "px";
|
|
|
+ private static final int MAX_WIDTH = 350;
|
|
|
+ private static final int MAX_HEIGHT = 300;
|
|
|
+ private static final String STYLE_WIDTH = "width:%spx;";
|
|
|
+ private static final String STYLE_HEIGHT = "height:%spx;";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 如果图片尺寸过大,则限制图片最大宽度高度
|
|
|
+ */
|
|
|
+ public static String reSizeImg(String html) {
|
|
|
+ if (html == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ Document document = Jsoup.parse(html);
|
|
|
+ Elements elements = document.getElementsByTag(TAG_IMG);
|
|
|
+ for (Element element : elements) {
|
|
|
+ int width = ImageUtils.parseValue(element.attr("style"), "width");
|
|
|
+ int height = ImageUtils.parseValue(element.attr("style"), "height");
|
|
|
+ //System.out.println(width + " - " + height);
|
|
|
+ element.removeAttr("width");
|
|
|
+ element.removeAttr("height");
|
|
|
+ String widthStr = "";
|
|
|
+ String heightStr = "";
|
|
|
+ if (width > 0) {
|
|
|
+ if (width > MAX_WIDTH) {
|
|
|
+ widthStr = String.format(STYLE_WIDTH, MAX_WIDTH);
|
|
|
+ } else {
|
|
|
+ widthStr = String.format(STYLE_WIDTH, width);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (height > 0) {
|
|
|
+ if (height > MAX_HEIGHT) {
|
|
|
+ heightStr = String.format(STYLE_HEIGHT, MAX_HEIGHT);
|
|
|
+ } else {
|
|
|
+ heightStr = String.format(STYLE_HEIGHT, height);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ element.attr("style", widthStr + heightStr);
|
|
|
+ }
|
|
|
+ String bodyHtml = document.body().html();
|
|
|
+ //System.out.println(bodyHtml);
|
|
|
+ return bodyHtml;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int parseValue(String styleValue, String attrName) {
|
|
|
+ if (styleValue == null || "".equals(styleValue)) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ styleValue = styleValue.replaceAll(" ", "");
|
|
|
+ String[] attrs = styleValue.toLowerCase().split(";");
|
|
|
+ for (String attr : attrs) {
|
|
|
+ if (!attr.startsWith(attrName)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String[] values = attr.split(":");
|
|
|
+ if (values.length > 1) {
|
|
|
+ String value = values[1].toLowerCase().replaceAll(UNIT, "");
|
|
|
+ return Integer.parseInt(value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ //do nothing
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int parseValue(String attrValue) {
|
|
|
+ if (attrValue == null || "".equals(attrValue)) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ attrValue = attrValue.replaceAll(" ", "");
|
|
|
+ String value = attrValue.toLowerCase().replaceAll(UNIT, "");
|
|
|
+ return Integer.parseInt(value);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String html = "<p><IMG src=\"abc\" style=\"width: 500PX; height:500PX;\"/><img src=\"xyz\" style=\"width:300px;\"/></p>";
|
|
|
+ ImageUtils.reSizeImg(html);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|