diff --git a/src/main/java/org/gcube/portlets/user/shareupdates/server/FilePreviewer.java b/src/main/java/org/gcube/portlets/user/shareupdates/server/FilePreviewer.java index eb329e4..78ed98a 100644 --- a/src/main/java/org/gcube/portlets/user/shareupdates/server/FilePreviewer.java +++ b/src/main/java/org/gcube/portlets/user/shareupdates/server/FilePreviewer.java @@ -1,9 +1,11 @@ package org.gcube.portlets.user.shareupdates.server; +import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Rectangle; +import java.awt.Transparency; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -56,7 +58,7 @@ public class FilePreviewer { private static final String PDF_DEFAULT_IMAGE = "default/pdf.png"; private static final String GENERICFILE_DEFAULT_IMAGE = "default/default_generic.png"; private static final String UPLOAD_LOCATION_LOCAL = System.getProperty("java.io.tmpdir"); - + /** * these are the extension for which I have an icon image preview */ @@ -155,13 +157,20 @@ public class FilePreviewer { out = new ByteArrayOutputStream(); - Thumbnails.of(path2Image) + // get the buffered image + BufferedImage bufferedImage = Thumbnails.of(path2Image).scale(1).asBufferedImage(); + + bufferedImage = changeBackgroundColorToWhite(bufferedImage); + + // write + Thumbnails.fromImages(Arrays.asList(bufferedImage)) .width(80) .outputFormat("jpg") .toOutputStream(out); + } catch (IOException e) { - e.printStackTrace(); + _log.warn("Thumbnail extraction failed for this reason: " + e.getMessage()); } String httpLink = getFTPManager().uploadImageOnFTPServer(new ByteArrayInputStream(out.toByteArray()), ImageType.JPG); _log.debug("\nFlushed, Image thumbnail available at: " + httpLink); @@ -169,6 +178,28 @@ public class FilePreviewer { return new LinkPreview(fileName, desc, httpUrl, mimeType, imagesUrl); } + private static BufferedImage changeBackgroundColorToWhite(BufferedImage bufferedImage){ + + BufferedImage changedImage = null; + try{ + // if the image has a transparent background color, it will be changed to white + if(bufferedImage.getColorModel().getTransparency() != Transparency.OPAQUE) { + _log.debug("Background color is going to be changed to white for the thumbnail"); + int w = bufferedImage.getWidth(); + int h = bufferedImage.getHeight(); + changedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); + Graphics2D g = changedImage.createGraphics(); + g.setColor(Color.WHITE); + g.fillRect(0,0,w,h); + g.drawRenderedImage(bufferedImage, null); + g.dispose(); + } + }catch(Exception e){ + _log.warn("Failed to change the transparent background to white for image"); + } + return changedImage == null ? bufferedImage : changedImage; + } + /** * * @param fileNameLabel thename of the file @@ -261,7 +292,7 @@ public class FilePreviewer { MediaType mediaType = detector.detect(stream, metadata); return mediaType.getBaseType().toString(); } - + /** * Check if a resource at a given url exists or not. If it exists, create a file in the temp dir * of the tomcat