/* neon-image - Image extensions to Neon Copyright (C) 2024 Ulrich Hilger This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ package de.uhilger.neon.image; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.nio.file.Files; import java.nio.file.Path; import java.util.Base64; import net.coobird.thumbnailator.Thumbnails; /** * Eine Klasse zum Herstellen verkleinerter sowie Base64-enkodierter * Fassungen eines Originalbildes * * @author Ulrich Hilger * @version 1, 13.06.2021 */ public class ImageWorker { /** * Diese String-Konstanten noetigenfalls in eine Konfigurationsdatei auslagern */ public static final String TN = "_tn"; // 120 public static final String KL = "_kl"; // 240 public static final String SM = "_sm"; // 500 public static final String MT = "_mt"; // 700 public static final String GR = "_gr"; // 1200 public static final String B64 = "_b64"; // Base64-Encoded public static final String JPG = ".jpg"; public static final String JPEG = ".jpeg"; public static final String PNG = ".png"; public void createImages(String baseDir, String fName, String uriStr) throws IOException { //if(uriStr.endsWith(ImageWorker.JPG) || uriStr.endsWith(ImageWorker.JPEG) || uriStr.endsWith(ImageWorker.PNG)) { File dir = new File(baseDir); File imgfile = new File(dir, fName); //ImageWorker actor = new ImageWorker(); if(uriStr.contains(TN)) { createImage(dir, fName, TN, 120, imgfile); } else if(uriStr.contains(KL)) { createImage(dir, fName, KL, 240, imgfile); } else if(uriStr.contains(SM)) { createImage(dir, fName, SM, 500, imgfile); } else if(uriStr.contains(MT)) { createImage(dir, fName, MT, 700, imgfile); } else if(uriStr.contains(GR)) { createImage(dir, fName, GR, 1200, imgfile); } else if(uriStr.contains(B64)) { File b64File = imgfile; String fromName = fName.replace(B64, ""); File fromfile = new File(dir, fromName); b64Image(fromfile, b64File); } //} } /** * Wenn b64 gewuenscht ist verweist tnfile auf dateiname_tn_b64.png Wenn b64 * nicht gewuenscht ist verweist tnfile auf dateiname_tn.png * * (oder _kl oder _sm usw. anstelle von _tn) * * @param dir * @param relname * @param indicator * @param gr * @param tnfile * @throws UnsupportedEncodingException * @throws IOException */ public void createImage(File dir, String relname, String indicator, int gr, File tnfile) throws UnsupportedEncodingException, IOException { File b64File = null; if (relname.contains(B64)) { b64File = tnfile; //String tnfilename = tnfile.getName(); relname = relname.replace(B64, ""); tnfile = new File(tnfile.getParentFile(), relname); } // _tn, usw. entfernen relname = relname.replace(indicator, ""); // die Original-Bilddatei File imgfile = new File(dir, URLDecoder.decode(relname, "utf-8")); //logger.fine("imgfile: " + imgfile + ", tnFile: " + tnfile); // Bild vom Original verkleinern if (imgfile.exists() && !tnfile.exists()) { Thumbnails.of(imgfile) .size(gr, gr) .keepAspectRatio(true) .outputQuality(0.7) .toFile(tnfile); /* BildVerkleinerer bv = new BildVerkleinerer(); bv.verkleinern(imgfile, tnfile, gr, gr, (float) 0.7); */ } b64Image(tnfile, b64File); } public void b64Image(File fromImg, File toFile) throws FileNotFoundException, IOException { //logger.fine("fromImg: " + fromImg.getAbsolutePath() + ", toFile: " + toFile.getAbsolutePath()); if (toFile != null && !toFile.exists()) { Path originalPath = fromImg.toPath(); Path targetPath = toFile.toPath(); Base64.Encoder mimeEncoder = Base64.getMimeEncoder(); try (OutputStream output = Files.newOutputStream(targetPath)) { Files.copy(originalPath, mimeEncoder.wrap(output)); } } } public void setImgSrc(Datei datei, String ext, File b64File) throws IOException { StringBuilder sb = new StringBuilder(); sb.append("data:image/"); //if(ext.equalsIgnoreCase(ImageWorker.JPEG) || ext.equalsIgnoreCase(ImageWorker.JPG)) { // sb.append("jpeg"); //} else if(ext.equalsIgnoreCase(ImageWorker.PNG)) { // sb.append("png"); //} sb.append(ext.toLowerCase()); sb.append(";base64,"); byte[] buf = new byte[4096]; InputStream is = new FileInputStream(b64File); int bytesRead = is.read(buf); while(bytesRead > -1) { String str = new String(buf, 0, bytesRead); sb.append(str); bytesRead = is.read(buf); } is.close(); datei.setImgSrc(sb.toString()); } }