1 package gboat2.base.view.components;
2
3 import gboat2.base.bridge.util.json.JsonUtil;
4
5 import java.io.Writer;
6
7 import org.apache.struts2.components.Component;
8 import org.apache.struts2.views.annotations.StrutsTag;
9 import org.apache.struts2.views.annotations.StrutsTagAttribute;
10
11 import com.fasterxml.jackson.databind.node.ObjectNode;
12 import com.opensymphony.xwork2.util.ValueStack;
13
14
15
16
17
18
19
20
21 @StrutsTag(
22 name = "attachResize",
23 tldTagClass = "gboat2.base.view.jsp.AttachResizeTag",
24 description = "上传图片之前在客户端调整要上传的图片的大小,只对 mineType 为 image/jpeg, image/png 的附件生效")
25 public class AttachResize extends Component {
26
27 protected String width;
28 protected String height;
29 protected String quality;
30 protected String crop;
31
32 public AttachResize(ValueStack stack) {
33 super(stack);
34 }
35
36 @Override
37 public boolean end(Writer writer, String body) {
38 ObjectNode resize = JsonUtil.generateMapper().createObjectNode();
39
40 if (width != null)
41 resize.put("width", (Integer) findValue(width, Integer.class));
42
43 if (height != null)
44 resize.put("height", (Integer) findValue(height, Integer.class));
45
46 if (quality != null)
47 resize.put("quality", (Integer) findValue(quality, Integer.class));
48
49 if (crop != null)
50 resize.put("crop", (Boolean) findValue(crop, Boolean.class));
51
52 findAncestor(Attach.class).addParameter("resize", resize);
53 return super.end(writer, body);
54 }
55
56 @StrutsTagAttribute(description = "要调整的目标宽度,如果原图的宽度大于该值,则将图片的宽度调整至该值", type = "Integer")
57 public void setWidth(String width) {
58 this.width = width;
59 }
60
61 @StrutsTagAttribute(description = "要调整的目标高度,如果原图的高度大于该值,则将图片的高度调整至该值", type = "Integer")
62 public void setHeight(String height) {
63 this.height = height;
64 }
65
66 @StrutsTagAttribute(description = "对 jpeg 图片的压缩质量, 值为 1--100 之间的整数", type = "Integer", defaultValue = "90")
67 public void setQuality(String quality) {
68 this.quality = quality;
69 }
70
71 @StrutsTagAttribute(description = "是否将图片的尺寸调整到精确的尺寸(width 和 height 指定的值),默认情况会按照比例进行调整(保持原宽高比)", defaultValue = "false")
72 public void setCrop(String crop) {
73 this.crop = crop;
74 }
75
76 }