Search in sources :

Example 1 with Cropping

use of com.enonic.xp.image.Cropping in project xp by enonic.

the class ImageContentProcessor method updateImageMetadata.

private ExtraDatas updateImageMetadata(final EditableContent editable) {
    final Media media = (Media) editable.source;
    final Attachment mediaAttachment = media.getMediaAttachment();
    if (mediaAttachment == null) {
        return editable.extraDatas;
    }
    final ByteSource binary = contentService.getBinary(editable.source.getId(), mediaAttachment.getBinaryReference());
    if (binary == null) {
        return editable.extraDatas;
    }
    final BufferedImage image = toBufferedImage(binary);
    final Cropping cropping = media.getCropping();
    final long byteSize = mediaAttachment.getSize();
    final long imageWidth;
    final long imageHeight;
    final long imageSize;
    if (cropping == null || cropping.isUnmodified()) {
        imageWidth = image.getWidth();
        imageHeight = image.getHeight();
        imageSize = imageWidth * imageHeight;
    } else {
        final BufferedImage croppedImage = cropImage(image, cropping);
        imageWidth = croppedImage.getWidth();
        imageHeight = croppedImage.getHeight();
        imageSize = imageWidth * imageHeight;
    }
    ExtraData extraData = editable.extraDatas.getMetadata(MediaInfo.IMAGE_INFO_METADATA_NAME);
    if (extraData != null) {
        final PropertyTree xData = extraData.getData();
        setLongProperty(xData, IMAGE_INFO_PIXEL_SIZE, imageSize);
        setLongProperty(xData, IMAGE_INFO_IMAGE_HEIGHT, imageHeight);
        setLongProperty(xData, IMAGE_INFO_IMAGE_WIDTH, imageWidth);
        setLongProperty(xData, MEDIA_INFO_BYTE_SIZE, byteSize);
    }
    return editable.extraDatas;
}
Also used : PropertyTree(com.enonic.xp.data.PropertyTree) Media(com.enonic.xp.content.Media) ByteSource(com.google.common.io.ByteSource) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) Attachment(com.enonic.xp.attachment.Attachment) ExtraData(com.enonic.xp.content.ExtraData) BufferedImage(java.awt.image.BufferedImage) Cropping(com.enonic.xp.image.Cropping)

Example 2 with Cropping

use of com.enonic.xp.image.Cropping in project xp by enonic.

the class ImageServiceImplTest method testReadImageWithCache.

@Test
public void testReadImageWithCache() throws IOException {
    Cropping cropping = Cropping.create().top(0.25).bottom(0.75).left(0.25).right(0.75).zoom(2).build();
    final ReadImageParams readImageParams = ReadImageParams.newImageParams().contentId(contentId).binaryReference(binaryReference).cropping(cropping).scaleSize(128).filterParam("blur(10)").mimeType("image/jpeg").backgroundColor(0xFF0000).quality(5).orientation(ImageOrientation.BottomLeft).build();
    ByteSource imageData = imageService.readImage(readImageParams);
    assertArrayEquals(ByteStreams.toByteArray(getClass().getResourceAsStream("processed.jpg")), imageData.read());
    Mockito.verify(imageFilter).apply(any());
    imageData = imageService.readImage(readImageParams);
    assertArrayEquals(ByteStreams.toByteArray(getClass().getResourceAsStream("processed.jpg")), imageData.read());
    Mockito.verify(imageFilter).apply(any());
}
Also used : ReadImageParams(com.enonic.xp.image.ReadImageParams) ByteSource(com.google.common.io.ByteSource) Cropping(com.enonic.xp.image.Cropping) Test(org.junit.jupiter.api.Test)

Aggregations

Cropping (com.enonic.xp.image.Cropping)2 ByteSource (com.google.common.io.ByteSource)2 Attachment (com.enonic.xp.attachment.Attachment)1 CreateAttachment (com.enonic.xp.attachment.CreateAttachment)1 ExtraData (com.enonic.xp.content.ExtraData)1 Media (com.enonic.xp.content.Media)1 PropertyTree (com.enonic.xp.data.PropertyTree)1 ReadImageParams (com.enonic.xp.image.ReadImageParams)1 BufferedImage (java.awt.image.BufferedImage)1 Test (org.junit.jupiter.api.Test)1