ThumbnailCreator.java
6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.image.Layer
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.ValueFactory
* org.apache.commons.io.IOUtils
*/
package com.day.cq.dam.commons.thumbnail;
import com.day.image.Layer;
import java.awt.Color;
import java.awt.Paint;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.List;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFactory;
import org.apache.commons.io.IOUtils;
@Deprecated
public class ThumbnailCreator {
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void createThumbnails(BufferedImage image, Node renditionsFolder, List<ThumbnailConfig> thumbnailConfigurations) throws IOException, RepositoryException {
File thumbnailtmpFile = null;
try {
thumbnailtmpFile = File.createTempFile("thumbnail", ".tmp");
for (ThumbnailConfig d : thumbnailConfigurations) {
FileOutputStream tout = null;
FileInputStream is = null;
int maxHeight = d.getHeight();
int maxWidth = d.getWidth();
try {
tout = new FileOutputStream(thumbnailtmpFile);
Layer thumbnailLayer = this.creatThumbnail(image, maxWidth, maxHeight, this.getExtension(renditionsFolder), d.isDoCenter());
thumbnailLayer.write("image/png", 0.8, (OutputStream)tout);
is = new FileInputStream(thumbnailtmpFile);
this.setThumbnail(renditionsFolder, is, d);
continue;
}
finally {
IOUtils.closeQuietly((InputStream)is);
IOUtils.closeQuietly((OutputStream)tout);
continue;
}
}
renditionsFolder.getSession().save();
}
finally {
if (thumbnailtmpFile != null) {
thumbnailtmpFile.delete();
}
}
}
public Layer creatThumbnail(BufferedImage image, int maxWidth, int maxHeight, String extension, boolean doCenter) {
Layer finalLayer = null;
Layer layer = new Layer(image);
int height = layer.getHeight();
int width = layer.getWidth();
Color bgColor = layer.getBackgroundColor();
if (height > maxHeight || width > maxWidth) {
int newWidth;
int newHeight;
if (height > width) {
newHeight = maxHeight;
newWidth = width * maxHeight / height;
if (newWidth > maxWidth) {
newWidth = maxWidth;
newHeight = height * maxWidth / width;
}
} else {
newWidth = maxWidth;
newHeight = height * maxWidth / width;
if (newHeight > maxHeight) {
newHeight = maxHeight;
newWidth = width * maxHeight / height;
}
}
layer.resize(newWidth, newHeight);
}
if ((layer.getHeight() < maxHeight || layer.getWidth() < maxWidth) && doCenter) {
if (bgColor == null) {
bgColor = Color.WHITE;
}
Color dummyColor = extension.equals("gif") ? new Color(-991024) : bgColor;
finalLayer = new Layer(maxWidth, maxHeight, (Paint)dummyColor);
finalLayer.setTransparency(dummyColor);
int y = (maxHeight - layer.getHeight()) / 2;
int x = (maxWidth - layer.getWidth()) / 2;
layer.setX(x);
layer.setY(y);
finalLayer.merge(layer);
}
if (finalLayer == null) {
return layer;
}
return finalLayer;
}
public ThumbnailConfig createThumbnailConfig() {
return new ThumbnailConfig();
}
private void setThumbnail(Node renditionsFolder, InputStream in, ThumbnailConfig thumbnailConf) throws RepositoryException {
if (renditionsFolder.hasNode(this.getThumbnailName(thumbnailConf))) {
Node content = renditionsFolder.getNode(this.getThumbnailName(thumbnailConf) + "/" + "jcr:content");
content.setProperty("jcr:mimeType", "image/png");
content.setProperty("jcr:data", content.getSession().getValueFactory().createBinary(in));
content.setProperty("jcr:lastModified", Calendar.getInstance());
} else {
Node file = renditionsFolder.addNode(this.getThumbnailName(thumbnailConf), "nt:file");
Node content = file.addNode("jcr:content", "nt:resource");
content.setProperty("jcr:mimeType", "image/png");
content.setProperty("jcr:data", content.getSession().getValueFactory().createBinary(in));
content.setProperty("jcr:lastModified", Calendar.getInstance());
}
}
private String getThumbnailName(ThumbnailConfig conf) {
String name = "cq5dam.thumbnail";
if (conf != null) {
name = name + "." + String.valueOf(conf.getWidth()) + "." + String.valueOf(conf.getHeight()) + (conf.isDoCenter() ? ".margin" : "");
}
name = name + ".png";
return name;
}
private String getExtension(Node node) throws RepositoryException {
String assetPath = node.getParent().getParent().getPath();
return assetPath.substring(assetPath.lastIndexOf(".") + 1);
}
public class ThumbnailConfig {
private int width;
private int height;
private boolean doCenter;
public ThumbnailConfig() {
this.doCenter = true;
}
public int getWidth() {
return this.width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return this.height;
}
public void setHeight(int height) {
this.height = height;
}
public boolean isDoCenter() {
return this.doCenter;
}
public void setDoCenter(boolean doCenter) {
this.doCenter = doCenter;
}
}
}