Skip to content

Commit

Permalink
Consistently encode snapshot image with format requested on the API.
Browse files Browse the repository at this point in the history
Previously, calling /api/snapshot.png rendered JPEG encoded images.
  • Loading branch information
luccioman committed Oct 18, 2017
1 parent 4da15db commit 285f0d6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
4 changes: 2 additions & 2 deletions htroot/api/snapshot.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static Object respond(final RequestHeader header, serverObjects post, fin
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;
Expand Down Expand Up @@ -286,7 +286,7 @@ public static Object respond(final RequestHeader header, serverObjects post, fin
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);
}
Expand Down
29 changes: 18 additions & 11 deletions source/net/yacy/cora/util/Html2Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
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;
Expand Down Expand Up @@ -140,17 +141,23 @@ private static boolean writeWkhtmltopdfInternal(final String url, final String p
}

/**
* 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
Expand All @@ -159,7 +166,7 @@ public static boolean pdf2image(File pdf, File image, int width, int height, int
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) { }
}
Expand Down Expand Up @@ -188,7 +195,7 @@ public static boolean pdf2image(File pdf, File image, int width, int height, int
//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);
Expand All @@ -200,7 +207,7 @@ public static boolean pdf2image(File pdf, File image, int width, int height, int
// 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) {
Expand Down
34 changes: 33 additions & 1 deletion test/java/net/yacy/cora/util/Html2ImageTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -38,8 +59,19 @@ public void testPdf2image() {
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());
}
}

Expand Down

0 comments on commit 285f0d6

Please sign in to comment.