Search in sources :

Example 26 with Image

use of org.structr.web.entity.Image in project structr by structr.

the class ImageConverterCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final String originalImageId = webSocketData.getId();
    final Map<String, Object> properties = webSocketData.getNodeData();
    final Image originalImage = (Image) getNode(originalImageId);
    final String format = (String) properties.get("format");
    final int width = (int) (long) properties.get("width");
    final int height = (int) (long) properties.get("height");
    final int offsetX = (int) (long) properties.get("offsetX");
    final int offsetY = (int) (long) properties.get("offsetY");
    if (originalImage != null) {
        final Thumbnail thumbnailData = ImageHelper.createCroppedImage(originalImage, width, height, offsetX, offsetY, format);
        if (thumbnailData != null) {
            final Integer tnWidth = thumbnailData.getWidth();
            final Integer tnHeight = thumbnailData.getHeight();
            byte[] data;
            try {
                data = thumbnailData.getBytes();
                final String thumbnailName = ImageHelper.getVariantName(originalImage.getName(), tnWidth, tnHeight, "_cropped_");
                // create image variant
                final Image imageVariant = ImageHelper.createImageNode(originalImage.getSecurityContext(), data, "image/" + Thumbnail.Format.png, Image.class, thumbnailName, false);
                // store in same parent folder
                imageVariant.setParent(originalImage.getParent());
            } catch (IOException | FrameworkException ex) {
                getWebSocket().send(MessageBuilder.status().code(400).message("Could not create converted image for " + originalImageId).build(), true);
            }
        } else {
            getWebSocket().send(MessageBuilder.status().code(400).message("Could not create converted image for " + originalImageId).build(), true);
        }
    } else {
        getWebSocket().send(MessageBuilder.status().code(400).message("No id of the original image given").build(), true);
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) IOException(java.io.IOException) Image(org.structr.web.entity.Image) Thumbnail(org.structr.web.common.ImageHelper.Thumbnail)

Aggregations

Image (org.structr.web.entity.Image)26 FrameworkException (org.structr.common.error.FrameworkException)18 Tx (org.structr.core.graph.Tx)12 PropertyMap (org.structr.core.property.PropertyMap)12 Test (org.junit.Test)9 StructrUiTest (org.structr.web.StructrUiTest)9 BufferedImage (java.awt.image.BufferedImage)8 IOException (java.io.IOException)7 File (org.structr.web.entity.File)6 AbstractFile (org.structr.web.entity.AbstractFile)5 App (org.structr.core.app.App)4 StructrApp (org.structr.core.app.StructrApp)4 SecurityContext (org.structr.common.SecurityContext)3 Relation (org.structr.core.entity.Relation)3 PropertyKey (org.structr.core.property.PropertyKey)3 Folder (org.structr.web.entity.Folder)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 ImageInputStream (javax.imageio.stream.ImageInputStream)2 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)2