WebEnabledImageCreator.java
6.3 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
/*
* 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.image.Layer
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* org.apache.commons.io.FileUtils
* org.apache.commons.io.IOUtils
* org.apache.commons.lang.StringUtils
* org.apache.sling.commons.mime.MimeTypeService
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.commons.util;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.commons.util.OrientationUtil;
import com.day.image.Layer;
import java.awt.Color;
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 javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.commons.mime.MimeTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Deprecated
public class WebEnabledImageCreator {
private static final Logger log = LoggerFactory.getLogger(WebEnabledImageCreator.class);
private static final String WEB_SPECIFIER = "web";
private Asset asset;
private MimeTypeService mimeTypeService;
@Deprecated
public WebEnabledImageCreator(Asset asset, MimeTypeService mimeTypeService) {
this.asset = asset;
this.mimeTypeService = mimeTypeService;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Deprecated
public void create(BufferedImage image, String defaultMimetype, String dimensions, String keepFormat, String qualityStr, boolean force) throws RepositoryException, IOException {
int maxWidth = this.getDimension(dimensions)[0];
int maxHeight = this.getDimension(dimensions)[1];
String oriMimeType = this.getMimeType(this.asset);
String mimetype = StringUtils.isNotBlank((String)oriMimeType) && keepFormat.contains(oriMimeType) ? oriMimeType : defaultMimetype;
double quality = mimetype.equals("image/gif") ? this.getQuality(255.0, qualityStr) : this.getQuality(1.0, qualityStr);
Layer layer = this.createImage(image, maxWidth, maxHeight);
OrientationUtil.adjustOrientation(this.asset, layer);
String renditionName = "cq5dam.web." + maxWidth + "." + maxHeight + "." + this.getExtension(mimetype);
if (StringUtils.contains((String)keepFormat, (String)oriMimeType)) {
if (image.getHeight() == layer.getHeight() && image.getWidth() == layer.getWidth() && !force) {
InputStream oriIs = null;
try {
oriIs = ((Node)this.asset.getOriginal().adaptTo(Node.class)).getProperty("jcr:content/jcr:data").getBinary().getStream();
this.asset.addRendition(renditionName, oriIs, mimetype);
}
finally {
IOUtils.closeQuietly((InputStream)oriIs);
}
}
this.saveImage(this.asset, layer, mimetype, quality, renditionName);
} else {
this.saveImage(this.asset, layer, mimetype, quality, renditionName);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void saveImage(Asset asset, Layer layer, String mimetype, double quality, String renditionName) throws IOException {
File tmpFile = File.createTempFile("web", "." + this.getExtension(mimetype));
FileOutputStream out = FileUtils.openOutputStream((File)tmpFile);
FileInputStream is = null;
try {
layer.write(mimetype, quality, (OutputStream)out);
is = FileUtils.openInputStream((File)tmpFile);
asset.addRendition(renditionName, (InputStream)is, mimetype);
}
finally {
IOUtils.closeQuietly((OutputStream)out);
IOUtils.closeQuietly((InputStream)is);
FileUtils.deleteQuietly((File)tmpFile);
}
}
protected Layer createImage(BufferedImage image, int maxWidth, int maxHeight) {
long startTime = System.currentTimeMillis();
Layer layer = new Layer(image);
int height = layer.getHeight();
int width = layer.getWidth();
if (height > maxHeight || width > maxWidth) {
int newHeight;
int newWidth;
if (height > width) {
newHeight = maxHeight;
newWidth = Math.round((float)width * (float)maxHeight / (float)height);
if (newWidth > maxWidth) {
newWidth = maxWidth;
newHeight = Math.round((float)height * (float)maxWidth / (float)width);
}
} else {
newWidth = maxWidth;
newHeight = Math.round((float)height * (float)maxWidth / (float)width);
if (newHeight > maxHeight) {
newHeight = maxHeight;
newWidth = Math.round((float)width * (float)maxHeight / (float)height);
}
}
layer.resize(newWidth, newHeight);
}
if (this.asset.getName().endsWith(".gif")) {
layer.setTransparency(new Color(-991024));
}
log.debug("createImage took " + (System.currentTimeMillis() - startTime) + "ms");
return layer;
}
protected String getExtension(String mimetype) {
return this.mimeTypeService.getExtension(mimetype);
}
protected String getMimeType(Asset asset) {
String name = asset.getName().toLowerCase();
return this.mimeTypeService.getMimeType(name);
}
protected Integer[] getDimension(String dimensions) {
if (dimensions != null) {
String[] splits = dimensions.split(":");
Integer[] d = new Integer[]{Integer.valueOf(splits[0]), Integer.valueOf(splits[1])};
return d;
}
return new Integer[]{1000, 1000};
}
protected double getQuality(double base, String qualityStr) {
int q = Integer.valueOf(qualityStr);
double res = base * (double)q / 100.0;
return res;
}
}