Example usage for com.liferay.portal.kernel.image ImageToolUtil crop

List of usage examples for com.liferay.portal.kernel.image ImageToolUtil crop

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.image ImageToolUtil crop.

Prototype

public static RenderedImage crop(RenderedImage renderedImage, int height, int width, int x, int y) 

Source Link

Usage

From source file:com.liferay.image.uploader.web.internal.portlet.action.UploadImageMVCActionCommand.java

License:Open Source License

protected FileEntry saveTempImageFileEntry(ActionRequest actionRequest) throws Exception {

    try {//w  w w  . ja  va  2s. c o m
        FileEntry tempFileEntry = UploadImageUtil.getTempImageFileEntry(actionRequest);

        try (InputStream tempImageStream = tempFileEntry.getContentStream()) {

            ImageBag imageBag = ImageToolUtil.read(tempImageStream);

            RenderedImage renderedImage = imageBag.getRenderedImage();

            String cropRegionJSON = ParamUtil.getString(actionRequest, "cropRegion");

            if (Validator.isNotNull(cropRegionJSON)) {
                JSONObject jsonObject = JSONFactoryUtil.createJSONObject(cropRegionJSON);

                int height = jsonObject.getInt("height");
                int width = jsonObject.getInt("width");
                int x = jsonObject.getInt("x");
                int y = jsonObject.getInt("y");

                if ((x == 0) && (y == 0) && (renderedImage.getHeight() == height)
                        && (renderedImage.getWidth() == width)) {

                    return tempFileEntry;
                }

                if ((height + y) > renderedImage.getHeight()) {
                    height = renderedImage.getHeight() - y;
                }

                if ((width + x) > renderedImage.getWidth()) {
                    width = renderedImage.getWidth() - x;
                }

                renderedImage = ImageToolUtil.crop(renderedImage, height, width, x, y);
            }

            byte[] bytes = ImageToolUtil.getBytes(renderedImage, imageBag.getType());

            ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

            File file = FileUtil.createTempFile(bytes);

            try {
                TempFileEntryUtil.deleteTempFileEntry(themeDisplay.getScopeGroupId(), themeDisplay.getUserId(),
                        UploadImageUtil.getTempImageFolderName(), getTempImageFileName(actionRequest));
            } catch (Exception e) {
            }

            return TempFileEntryUtil.addTempFileEntry(themeDisplay.getScopeGroupId(), themeDisplay.getUserId(),
                    UploadImageUtil.getTempImageFolderName(), getTempImageFileName(actionRequest), file,
                    tempFileEntry.getMimeType());
        }
    } catch (NoSuchFileEntryException nsfee) {
        throw new UploadException(nsfee);
    } catch (NoSuchRepositoryException nsre) {
        throw new UploadException(nsre);
    }
}