ThumbnailGenerator.java
7.96 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.dam.api.Asset
* com.day.cq.dam.api.Rendition
* com.day.cq.dam.api.thumbnail.ThumbnailConfig
* com.day.image.Layer
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.io.FileUtils
* org.apache.commons.io.IOUtils
* org.apache.commons.lang.StringUtils
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.commons.thumbnail;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.api.thumbnail.ThumbnailConfig;
import com.day.cq.dam.commons.thumbnail.ThumbnailConfigImpl;
import com.day.cq.dam.commons.util.DamUtil;
import com.day.cq.dam.commons.util.OrientationUtil;
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.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Deprecated
public class ThumbnailGenerator {
private static final Logger log = LoggerFactory.getLogger(ThumbnailGenerator.class);
private final Asset asset;
private final BufferedImage image;
@Deprecated
public ThumbnailGenerator(Asset asset, BufferedImage image) {
this.asset = asset;
this.image = image;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Deprecated
public void generate(Collection<ThumbnailConfig> configs) throws IOException, RepositoryException {
File tmpFile = null;
boolean oldBatchMode = this.asset.isBatchMode();
this.asset.setBatchMode(true);
try {
tmpFile = File.createTempFile("thumbnail", ".tmp");
ArrayList<ThumbnailConfig> cfg = new ArrayList<ThumbnailConfig>(configs);
Collections.sort(cfg, new Comparator<ThumbnailConfig>(){
@Override
public int compare(ThumbnailConfig o1, ThumbnailConfig o2) {
if (o1.getWidth() == o2.getWidth()) {
return o2.getHeight() - o1.getHeight();
}
return o2.getWidth() - o1.getWidth();
}
});
Layer layer = null;
for (ThumbnailConfig config : cfg) {
if (layer == null || config.getWidth() > layer.getWidth() || config.getHeight() > layer.getHeight()) {
if (layer != null) {
layer.dispose();
}
layer = new Layer(this.image);
OrientationUtil.adjustOrientation(this.asset, layer);
}
Layer tLayer = this.createThumbnail(layer, config);
FileOutputStream out = null;
FileInputStream in = null;
try {
out = FileUtils.openOutputStream((File)tmpFile);
tLayer.write("image/png", 0.8, (OutputStream)out);
IOUtils.closeQuietly((OutputStream)out);
in = FileUtils.openInputStream((File)tmpFile);
this.asset.addRendition(DamUtil.getThumbnailName(config), (InputStream)in, "image/png");
}
finally {
IOUtils.closeQuietly((InputStream)in);
IOUtils.closeQuietly((OutputStream)out);
}
if (tLayer == layer) continue;
tLayer.dispose();
}
try {
if (!this.asset.isBatchMode()) {
((Node)this.asset.adaptTo(Node.class)).getSession().save();
}
}
catch (RepositoryException e) {
String msg = "generate: error while saving changes for asset [" + this.asset.getPath() + "]: ";
log.debug(msg, (Throwable)e);
throw new RepositoryException(msg, (Throwable)e);
}
}
finally {
this.asset.setBatchMode(oldBatchMode);
FileUtils.deleteQuietly((File)tmpFile);
}
}
@Deprecated
public static Collection<ThumbnailConfig> parseConfig(String[] args) {
HashSet<ThumbnailConfig> configs = new HashSet<ThumbnailConfig>();
for (String arg : args) {
ThumbnailConfig config = ThumbnailGenerator.parseConfig(arg);
if (null == config) continue;
configs.add(config);
}
return configs;
}
@Deprecated
public static ThumbnailConfig parseConfig(String str) {
ThumbnailConfigImpl config = null;
str = str.trim();
String[] fragments = (str = StringUtils.replaceEach((String)str, (String[])new String[]{"[", "]"}, (String[])new String[]{"", ""})).split(":");
if (fragments.length >= 2) {
try {
Integer width = Integer.valueOf(fragments[0]);
Integer height = Integer.valueOf(fragments[1]);
boolean doCenter = false;
if (fragments.length > 2) {
doCenter = Boolean.valueOf(fragments[2]);
}
config = new ThumbnailConfigImpl(width, height, doCenter);
}
catch (NumberFormatException e) {
log.warn("parseConfig: cannot parse, invalid width/height specified in config [{}]: ", (Object)str, (Object)e);
}
} else {
log.warn("parseConfig: cannot parse, insufficient arguments in config [{}].", (Object)str);
}
return config;
}
@Deprecated
public static Collection<ThumbnailConfig> getConfigs(Collection<Integer[]> dimensions) {
HashSet<ThumbnailConfig> configs = new HashSet<ThumbnailConfig>();
for (Integer[] dimension : dimensions) {
configs.add(new ThumbnailConfigImpl(dimension[0], dimension[1], false));
}
return configs;
}
private Layer createThumbnail(Layer layer, ThumbnailConfig config) {
Layer finalLayer = null;
int maxWidth = config.getWidth();
int maxHeight = config.getHeight();
boolean doCenter = config.doCenter();
int width = layer.getWidth();
int height = layer.getHeight();
Color bgColor = layer.getBackgroundColor();
if (height > maxHeight || width > maxWidth) {
int newHeight;
int newWidth;
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, true);
}
if ((layer.getHeight() < maxHeight || layer.getWidth() < maxWidth) && doCenter) {
Color bg = null != bgColor ? bgColor : Color.WHITE;
finalLayer = new Layer(maxWidth, maxHeight, (Paint)bg);
finalLayer.setTransparency(bg);
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;
}
}