This is an automated email from the git hooks/post-receive script. New commit to branch feature/createPreviewRessource in repository pollen. See http://git.chorem.org/pollen.git commit 58fc68e808b675a30276298e507fdceef0bd10e0 Author: Adrien Garandel <a.garandel@dralagen.fr> Date: Tue Aug 12 17:47:22 2014 +0200 resize picture for preview --- .../pollen/rest/api/v1/PollenResourceApi.java | 42 ++++++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollenResourceApi.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollenResourceApi.java index 7db0f32..0b5f931 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollenResourceApi.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollenResourceApi.java @@ -1,5 +1,6 @@ package org.chorem.pollen.rest.api.v1; +import org.apache.commons.io.IOUtils; import org.chorem.pollen.persistence.entity.PollenResource; import org.chorem.pollen.services.bean.*; import org.chorem.pollen.services.service.PollenResourceService; @@ -7,7 +8,13 @@ import org.debux.webmotion.server.WebMotionController; import org.debux.webmotion.server.call.UploadFile; import org.debux.webmotion.server.render.Render; -import java.io.FileNotFoundException; +import javax.imageio.ImageIO; +import javax.imageio.ImageReader; +import javax.imageio.stream.ImageInputStream; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.*; +import java.util.Iterator; /** * Created on 10/07/14. @@ -23,11 +30,40 @@ public class PollenResourceApi extends WebMotionController { return renderStream(resource.getResourceContent(), resource.getContentType()); } - public Render getPreviewResource(PollenResourceService pollenResourceService, PollenEntityId<PollenResource> resourceId) { + public Render getPreviewResource(PollenResourceService pollenResourceService, PollenEntityId<PollenResource> resourceId) throws IOException { ResourceStreamBean resource = pollenResourceService.getResource(resourceId.getEntityId()); - return renderStream(resource.getResourceContent(), resource.getContentType()); + ImageInputStream iis = ImageIO.createImageInputStream(resource.getResourceContent()); + + Iterator<ImageReader> readers = ImageIO.getImageReaders(iis); + if (!readers.hasNext()) { + throw new IOException("Unreadable source. No format can be found."); + } + + ImageReader reader = readers.next(); + String format = reader.getFormatName(); + + BufferedImage source = ImageIO.read(iis); + + int width, height; + if (source.getWidth() > source.getHeight()) { + width = 200; + height = width * source.getHeight() / source.getWidth(); + } else { + height = 200; + width = height * source.getWidth() / source.getHeight(); + } + + BufferedImage destination = new BufferedImage(width, height, source.getType()); + Graphics2D g = destination.createGraphics(); + g.drawImage(source, 0, 0, width, height, null); + g.dispose(); + + ByteArrayOutputStream output = new ByteArrayOutputStream(); + ImageIO.write(destination, format, output); + + return renderStream(new ByteArrayInputStream(output.toByteArray()), resource.getContentType()); } public ResourceMetaBean getMetaResource(PollenResourceService pollenResourceService, PollenEntityId<PollenResource> resourceId) { -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.