commit | author | age
|
ef2fb2
|
1 |
/* |
U |
2 |
neon-image - Image extensions to Neon |
|
3 |
Copyright (C) 2024 Ulrich Hilger |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
package de.uhilger.neon.image; |
|
19 |
|
|
20 |
import java.io.File; |
|
21 |
import java.io.FileInputStream; |
|
22 |
import java.io.FileNotFoundException; |
|
23 |
import java.io.IOException; |
|
24 |
import java.io.InputStream; |
|
25 |
import java.io.OutputStream; |
|
26 |
import java.io.UnsupportedEncodingException; |
|
27 |
import java.net.URLDecoder; |
|
28 |
import java.nio.file.Files; |
|
29 |
import java.nio.file.Path; |
|
30 |
import java.util.Base64; |
|
31 |
import net.coobird.thumbnailator.Thumbnails; |
|
32 |
|
|
33 |
/** |
|
34 |
* Eine Klasse zum Herstellen verkleinerter sowie Base64-enkodierter |
|
35 |
* Fassungen eines Originalbildes |
|
36 |
* |
|
37 |
* @author Ulrich Hilger |
|
38 |
* @version 1, 13.06.2021 |
|
39 |
*/ |
|
40 |
public class ImageWorker { |
|
41 |
|
|
42 |
/** |
|
43 |
* Diese String-Konstanten noetigenfalls in eine Konfigurationsdatei auslagern |
|
44 |
*/ |
|
45 |
public static final String TN = "_tn"; // 120 |
|
46 |
public static final String KL = "_kl"; // 240 |
|
47 |
public static final String SM = "_sm"; // 500 |
|
48 |
public static final String MT = "_mt"; // 700 |
|
49 |
public static final String GR = "_gr"; // 1200 |
|
50 |
|
|
51 |
public static final String B64 = "_b64"; // Base64-Encoded |
|
52 |
|
|
53 |
public static final String JPG = ".jpg"; |
|
54 |
public static final String JPEG = ".jpeg"; |
|
55 |
public static final String PNG = ".png"; |
|
56 |
|
|
57 |
public void createImages(String baseDir, String fName, String uriStr) throws IOException { |
|
58 |
if(uriStr.endsWith(ImageWorker.JPG) || uriStr.endsWith(ImageWorker.JPEG) || uriStr.endsWith(ImageWorker.PNG)) { |
|
59 |
File dir = new File(baseDir); |
|
60 |
File imgfile = new File(dir, fName); |
|
61 |
ImageWorker actor = new ImageWorker(); |
|
62 |
if(uriStr.contains(ImageWorker.TN)) { |
|
63 |
actor.createImage(dir, fName, ImageWorker.TN, 120, imgfile); |
|
64 |
} else if(uriStr.contains(ImageWorker.KL)) { |
|
65 |
actor.createImage(dir, fName, ImageWorker.KL, 240, imgfile); |
|
66 |
} else if(uriStr.contains(ImageWorker.SM)) { |
|
67 |
actor.createImage(dir, fName, ImageWorker.SM, 500, imgfile); |
|
68 |
} else if(uriStr.contains(ImageWorker.MT)) { |
|
69 |
actor.createImage(dir, fName, ImageWorker.MT, 700, imgfile); |
|
70 |
} else if(uriStr.contains(ImageWorker.GR)) { |
|
71 |
actor.createImage(dir, fName, ImageWorker.GR, 1200, imgfile); |
|
72 |
} else if(uriStr.contains(ImageWorker.B64)) { |
|
73 |
File b64File = imgfile; |
|
74 |
String fromName = fName.replace(ImageWorker.B64, ""); |
|
75 |
File fromfile = new File(dir, fromName); |
|
76 |
actor.b64Image(fromfile, b64File); |
|
77 |
} |
|
78 |
} |
|
79 |
} |
|
80 |
|
|
81 |
/** |
|
82 |
* Wenn b64 gewuenscht ist verweist tnfile auf dateiname_tn_b64.png Wenn b64 |
|
83 |
* nicht gewuenscht ist verweist tnfile auf dateiname_tn.png |
|
84 |
* |
|
85 |
* (oder _kl oder _sm usw. anstelle von _tn) |
|
86 |
* |
|
87 |
* @param dir |
|
88 |
* @param relname |
|
89 |
* @param indicator |
|
90 |
* @param gr |
|
91 |
* @param tnfile |
|
92 |
* @throws UnsupportedEncodingException |
|
93 |
* @throws IOException |
|
94 |
*/ |
|
95 |
public void createImage(File dir, String relname, String indicator, int gr, File tnfile) |
|
96 |
throws UnsupportedEncodingException, IOException { |
|
97 |
File b64File = null; |
|
98 |
if (relname.contains(B64)) { |
|
99 |
b64File = tnfile; |
|
100 |
//String tnfilename = tnfile.getName(); |
|
101 |
relname = relname.replace(B64, ""); |
|
102 |
tnfile = new File(tnfile.getParentFile(), relname); |
|
103 |
} |
|
104 |
// _tn, usw. entfernen |
|
105 |
relname = relname.replace(indicator, ""); |
|
106 |
// die Original-Bilddatei |
|
107 |
File imgfile = new File(dir, URLDecoder.decode(relname, "utf-8")); |
|
108 |
//logger.fine("imgfile: " + imgfile + ", tnFile: " + tnfile); |
|
109 |
// Bild vom Original verkleinern |
|
110 |
if (imgfile.exists() && !tnfile.exists()) { |
|
111 |
|
|
112 |
Thumbnails.of(imgfile) |
|
113 |
.size(gr, gr) |
|
114 |
.keepAspectRatio(true) |
|
115 |
.outputQuality(0.7) |
|
116 |
.toFile(tnfile); |
|
117 |
/* |
|
118 |
BildVerkleinerer bv = new BildVerkleinerer(); |
|
119 |
bv.verkleinern(imgfile, tnfile, gr, gr, (float) 0.7); |
|
120 |
*/ |
|
121 |
} |
|
122 |
b64Image(tnfile, b64File); |
|
123 |
} |
|
124 |
|
|
125 |
public void b64Image(File fromImg, File toFile) throws FileNotFoundException, IOException { |
|
126 |
//logger.fine("fromImg: " + fromImg.getAbsolutePath() + ", toFile: " + toFile.getAbsolutePath()); |
|
127 |
if (toFile != null && !toFile.exists()) { |
|
128 |
Path originalPath = fromImg.toPath(); |
|
129 |
Path targetPath = toFile.toPath(); |
|
130 |
Base64.Encoder mimeEncoder = Base64.getMimeEncoder(); |
|
131 |
try (OutputStream output = Files.newOutputStream(targetPath)) { |
|
132 |
Files.copy(originalPath, mimeEncoder.wrap(output)); |
|
133 |
} |
|
134 |
} |
|
135 |
} |
|
136 |
|
|
137 |
|
|
138 |
public void setImgSrc(Datei datei, String ext, File b64File) throws IOException { |
|
139 |
StringBuilder sb = new StringBuilder(); |
|
140 |
sb.append("data:image/"); |
|
141 |
if(ext.equalsIgnoreCase(ImageWorker.JPEG) || ext.equalsIgnoreCase(ImageWorker.JPG)) { |
|
142 |
sb.append("jpeg"); |
|
143 |
} else if(ext.equalsIgnoreCase(ImageWorker.PNG)) { |
|
144 |
sb.append("png"); |
|
145 |
} |
|
146 |
sb.append(";base64,"); |
|
147 |
byte[] buf = new byte[4096]; |
|
148 |
InputStream is = new FileInputStream(b64File); |
|
149 |
int bytesRead = is.read(buf); |
|
150 |
while(bytesRead > -1) { |
|
151 |
String str = new String(buf, 0, bytesRead); |
|
152 |
sb.append(str); |
|
153 |
bytesRead = is.read(buf); |
|
154 |
} |
|
155 |
is.close(); |
|
156 |
datei.setImgSrc(sb.toString()); |
|
157 |
} |
|
158 |
|
|
159 |
} |