001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.servlet.taglib.ui;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.image.ImageBag;
019    import com.liferay.portal.kernel.image.ImageToolUtil;
020    import com.liferay.portal.kernel.json.JSONFactoryUtil;
021    import com.liferay.portal.kernel.json.JSONObject;
022    import com.liferay.portal.kernel.repository.model.FileEntry;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
025    
026    import java.awt.image.RenderedImage;
027    
028    import java.io.IOException;
029    
030    /**
031     * @author Sergio Gonz??lez
032     */
033    public class ImageSelector {
034    
035            public ImageSelector(long imageId) {
036                    _imageId = imageId;
037    
038                    _imageCropRegion = null;
039                    _imageURL = null;
040            }
041    
042            public ImageSelector(long imageId, String imageCropRegion) {
043                    _imageId = imageId;
044                    _imageCropRegion = imageCropRegion;
045    
046                    _imageURL = null;
047            }
048    
049            public ImageSelector(
050                    long imageId, String imageURL, String imageCropRegion) {
051    
052                    _imageId = imageId;
053                    _imageURL = imageURL;
054                    _imageCropRegion = imageCropRegion;
055            }
056    
057            public ImageSelector(String imageURL, String imageCropRegion) {
058                    _imageURL = imageURL;
059                    _imageCropRegion = imageCropRegion;
060    
061                    _imageId = 0;
062            }
063    
064            public byte[] getCroppedImageBytes() throws IOException, PortalException {
065                    if (!isCroppedImage()) {
066                            return null;
067                    }
068    
069                    FileEntry fileEntry = PortletFileRepositoryUtil.getPortletFileEntry(
070                            _imageId);
071    
072                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
073                            _imageCropRegion);
074    
075                    int height = jsonObject.getInt("height");
076                    int width = jsonObject.getInt("width");
077                    int x = jsonObject.getInt("x");
078                    int y = jsonObject.getInt("y");
079    
080                    if ((x > 0) || (y > 0) || (width > 0) || (height > 0)) {
081                            ImageBag imageBag = ImageToolUtil.read(
082                                    fileEntry.getContentStream());
083    
084                            RenderedImage renderedImage = imageBag.getRenderedImage();
085    
086                            renderedImage = ImageToolUtil.crop(
087                                    renderedImage, height, width, x, y);
088    
089                            return ImageToolUtil.getBytes(renderedImage, imageBag.getType());
090                    }
091    
092                    return null;
093            }
094    
095            public String getImageCropRegion() {
096                    return _imageCropRegion;
097            }
098    
099            public long getImageId() {
100                    return _imageId;
101            }
102    
103            public String getImageURL() {
104                    return _imageURL;
105            }
106    
107            public String getMimeType() throws PortalException {
108                    if (_imageId == 0) {
109                            return null;
110                    }
111    
112                    FileEntry fileEntry = PortletFileRepositoryUtil.getPortletFileEntry(
113                            _imageId);
114    
115                    return fileEntry.getMimeType();
116            }
117    
118            public String getTitle() throws PortalException {
119                    if (_imageId == 0) {
120                            return null;
121                    }
122    
123                    FileEntry fileEntry = PortletFileRepositoryUtil.getPortletFileEntry(
124                            _imageId);
125    
126                    return fileEntry.getTitle();
127            }
128    
129            public boolean isCroppedImage() {
130                    if ((_imageId == 0) || Validator.isNull(_imageCropRegion)) {
131                            return false;
132                    }
133    
134                    return true;
135            }
136    
137            public boolean isRemoveSmallImage() {
138                    if ((_imageId == 0) && Validator.isNull(_imageURL)) {
139                            return true;
140                    }
141    
142                    return false;
143            }
144    
145            private final String _imageCropRegion;
146            private final long _imageId;
147            private final String _imageURL;
148    
149    }