Skip to content

Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance

Consistently encode snapshot image with format requested on the API.

luccioman285f0d6a3947
diff --git a/htroot/api/snapshot.java b/htroot/api/snapshot.java
index 5ddf7cfba..b734d65b4 100644
--- a/htroot/api/snapshot.java
+++ b/htroot/api/snapshot.java
@@ -106,7 +106,7 @@ public class snapshot {
final boolean xml = ext.equals("xml");
final boolean pdf = ext.equals("pdf");
if (pdf && !authenticated) return null;
- final boolean pngjpg = ext.equals("png") || ext.equals("jpg");
+ final boolean pngjpg = ext.equals("png") || ext.equals(DEFAULT_EXT);
String urlhash = post.get("urlhash", "");
String url = post.get("url", "");
DigestURL durl = null;
@@ -286,7 +286,7 @@ public class snapshot {
int width = Math.min(post.getInt("width", DEFAULT_WIDTH), DEFAULT_WIDTH);
int height = Math.min(post.getInt("height", DEFAULT_HEIGHT), DEFAULT_HEIGHT);
String imageFileStub = pdfFile.getAbsolutePath(); imageFileStub = imageFileStub.substring(0, imageFileStub.length() - 3); // cut off extension
- File imageFile = new File(imageFileStub + DEFAULT_WIDTH + "." + DEFAULT_HEIGHT + "." + DEFAULT_EXT);
+ File imageFile = new File(imageFileStub + DEFAULT_WIDTH + "." + DEFAULT_HEIGHT + "." + ext);
if (!imageFile.exists() && authenticated) {
Html2Image.pdf2image(pdfFile, imageFile, DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_DENSITY, DEFAULT_QUALITY);
}
diff --git a/source/net/yacy/cora/util/Html2Image.java b/source/net/yacy/cora/util/Html2Image.java
index d3de36ade..217817744 100644
--- a/source/net/yacy/cora/util/Html2Image.java
+++ b/source/net/yacy/cora/util/Html2Image.java
@@ -42,6 +42,7 @@ import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.ImageView;
+import net.yacy.cora.document.id.MultiProtocolURL;
import net.yacy.document.ImageParser;
import net.yacy.kelondro.util.FileUtils;
import net.yacy.kelondro.util.OS;
@@ -140,17 +141,23 @@ public class Html2Image {
}
/**
- * convert a pdf (first page) to an image. proper values are i.e. width = 1024, height = 1024, density = 300, quality = 75
+ * Convert a pdf (first page) to an image. Proper values are i.e. width = 1024, height = 1024, density = 300, quality = 75
* using internal pdf library or external command line tool on linux or mac
- * @param pdf input pdf file
- * @param image output jpg file
- * @param width
- * @param height
+ * @param pdf input pdf file. Must not be null.
+ * @param image output image file. Must not be null, and should end with ".jpg" or ".png".
+ * @param width output width in pixels
+ * @param height output height in pixels
* @param density (dpi)
- * @param quality
- * @return
+ * @param quality JPEG/PNG compression level
+ * @return true when the ouput image file was successfully written.
*/
- public static boolean pdf2image(File pdf, File image, int width, int height, int density, int quality) {
+ public static boolean pdf2image(final File pdf, final File image, final int width, final int height, final int density, final int quality) {
+ /* Deduce the ouput image format from the file extension */
+ String imageFormat = MultiProtocolURL.getFileExtension(image.getName());
+ if(imageFormat.isEmpty()) {
+ /* Use JPEG as a default fallback */
+ imageFormat = "jpg";
+ }
final File convert = convertMac1.exists() ? convertMac1 : convertMac2.exists() ? convertMac2 : convertDebian;
// convert pdf to jpg using internal pdfbox capability
@@ -159,7 +166,7 @@ public class Html2Image {
PDDocument pdoc = PDDocument.load(pdf);
BufferedImage bi = new PDFRenderer(pdoc).renderImageWithDPI(0, density, ImageType.RGB);
- return ImageIO.write(bi, "jpg", image);
+ return ImageIO.write(bi, imageFormat, image);
} catch (IOException ex) { }
}
@@ -188,7 +195,7 @@ public class Html2Image {
//ConcurrentLog.warn("Html2Image", "failed to create image with command: " + commandx);
message = OS.execSynchronous(commandx);
for (String m: message) ConcurrentLog.warn("Html2Image", ">> " + m);
- // now we must read and convert this file to a jpg with the target size 1024x1024
+ // now we must read and convert this file to the target format with the target size 1024x1024
try {
File newPngFile = new File(pngFile.getAbsolutePath() + ".png");
pngFile.renameTo(newPngFile);
@@ -200,7 +207,7 @@ public class Html2Image {
// finally write the image
final BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
bi.createGraphics().drawImage(scaled, 0, 0, width, height, null);
- ImageIO.write(bi, "jpg", image);
+ ImageIO.write(bi, imageFormat, image);
newPngFile.delete();
return image.exists();
} catch (IOException e) {
diff --git a/test/java/net/yacy/cora/util/Html2ImageTest.java b/test/java/net/yacy/cora/util/Html2ImageTest.java
index 4274cd6df..2e92c2e23 100644
--- a/test/java/net/yacy/cora/util/Html2ImageTest.java
+++ b/test/java/net/yacy/cora/util/Html2ImageTest.java
@@ -1,3 +1,24 @@
+// Html2ImageTest.java
+// Copyright 2016,2017 by reger; https://github.com/reger24 luccioman; https://github.com/luccioman
+//
+// This is a part of YaCy, a peer-to-peer based web search engine
+//
+// LICENSE
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 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 General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
package net.yacy.cora.util;
import java.io.File;
@@ -38,8 +59,19 @@ public class Html2ImageTest {
if (jpgfile.exists()) {
jpgfile.delete();
}
- Html2Image.pdf2image(pdffile, jpgfile, 1024, 1024, 300, 75);
+ assertTrue(Html2Image.pdf2image(pdffile, jpgfile, 1024, 1024, 300, 75));
assertTrue(jpgfile.exists());
+ assertTrue(jpgfile.length() > 0);
+ System.out.println("Test image file successfully written : " + jpgfile.getAbsolutePath());
+
+ final File pngFile = new File("test/DATA", pdffilename + ".png");
+ if (pngFile.exists()) {
+ pngFile.delete();
+ }
+ assertTrue(Html2Image.pdf2image(pdffile, pngFile, 1024, 1024, 300, 75));
+ assertTrue(pngFile.exists());
+ assertTrue(pngFile.length() > 0);
+ System.out.println("Test image file successfully written : " + pngFile.getAbsolutePath());
}
}