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.image; 016 017 import aQute.bnd.annotation.ProviderType; 018 019 import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission; 020 import com.liferay.portal.model.Image; 021 022 import java.awt.image.BufferedImage; 023 import java.awt.image.RenderedImage; 024 025 import java.io.File; 026 import java.io.IOException; 027 import java.io.InputStream; 028 import java.io.OutputStream; 029 030 import java.util.concurrent.Future; 031 032 /** 033 * The Image utility class. 034 * 035 * @author Brian Wing Shun Chan 036 * @author Alexander Chow 037 */ 038 @ProviderType 039 public class ImageToolUtil { 040 041 /** 042 * Returns the CMYK image converted to RGB using ImageMagick. This must be 043 * run against the original <code>byte[]</code> and not one extracted from a 044 * {@link java.awt.image.RenderedImage}. The latter may potentially have 045 * been already been read incorrectly. 046 * 047 * @param bytes the image to convert 048 * @param type the image type (e.g., "gif", "jpg", etc.) 049 * @return the asynchronous process converting the image or <code>null 050 * </code> if ImageMagick was disabled or if the conversion could 051 * not be completed. The conversion may not complete if (1) the 052 * image was not in the CMYK colorspace to begin with or (2) there 053 * was an error in the conversion process. 054 */ 055 public static Future<RenderedImage> convertCMYKtoRGB( 056 byte[] bytes, String type) { 057 058 return getImageTool().convertCMYKtoRGB(bytes, type); 059 } 060 061 /** 062 * Returns the image converted to the type. 063 * 064 * @param sourceImage the image to convert 065 * @param type the image type to convert to (e.g., "gif", "jpg", etc.) 066 * @return the converted image 067 */ 068 public static BufferedImage convertImageType( 069 BufferedImage sourceImage, int type) { 070 071 return getImageTool().convertImageType(sourceImage, type); 072 } 073 074 public static RenderedImage crop( 075 RenderedImage renderedImage, int height, int width, int x, int y) { 076 077 return getImageTool().crop(renderedImage, height, width, x, y); 078 } 079 080 /** 081 * Encodes the image using the GIF format. 082 * 083 * @param renderedImage the image to encode 084 * @param os the stream to write to 085 * @throws IOException if an IO exception occurred 086 */ 087 public static void encodeGIF(RenderedImage renderedImage, OutputStream os) 088 throws IOException { 089 090 getImageTool().encodeGIF(renderedImage, os); 091 } 092 093 /** 094 * Encodes the image using the WBMP format. 095 * 096 * @param renderedImage the image to encode 097 * @param os the stream to write to 098 * @throws IOException if an IO exception occurred 099 */ 100 public static void encodeWBMP(RenderedImage renderedImage, OutputStream os) 101 throws IOException { 102 103 getImageTool().encodeWBMP(renderedImage, os); 104 } 105 106 /** 107 * Returns the rendered image as a {@link java.awt.image.BufferedImage}. 108 * 109 * @param renderedImage the original image 110 * @return the converted image 111 */ 112 public static BufferedImage getBufferedImage(RenderedImage renderedImage) { 113 return getImageTool().getBufferedImage(renderedImage); 114 } 115 116 /** 117 * Returns the image as a <code>byte[]</code>. 118 * 119 * @param renderedImage the image to read 120 * @param contentType the content type (e.g., "image/jpeg") or image type 121 * (e.g., "jpg") to use during encoding 122 * @return the encoded image 123 * @throws IOException if an IO exception occurred 124 */ 125 public static byte[] getBytes( 126 RenderedImage renderedImage, String contentType) 127 throws IOException { 128 129 return getImageTool().getBytes(renderedImage, contentType); 130 } 131 132 public static Image getDefaultCompanyLogo() { 133 return getImageTool().getDefaultCompanyLogo(); 134 } 135 136 public static Image getDefaultOrganizationLogo() { 137 return getImageTool().getDefaultOrganizationLogo(); 138 } 139 140 public static Image getDefaultSpacer() { 141 return getImageTool().getDefaultSpacer(); 142 } 143 144 public static Image getDefaultUserFemalePortrait() { 145 return getImageTool().getDefaultUserFemalePortrait(); 146 } 147 148 public static Image getDefaultUserMalePortrait() { 149 return getImageTool().getDefaultUserMalePortrait(); 150 } 151 152 public static Image getImage(byte[] bytes) throws IOException { 153 return getImageTool().getImage(bytes); 154 } 155 156 public static Image getImage(File file) throws IOException { 157 return getImageTool().getImage(file); 158 } 159 160 public static Image getImage(InputStream is) throws IOException { 161 return getImageTool().getImage(is); 162 } 163 164 public static Image getImage(InputStream is, boolean cleanUpStream) 165 throws IOException { 166 167 return getImageTool().getImage(is, cleanUpStream); 168 } 169 170 public static ImageTool getImageTool() { 171 PortalRuntimePermission.checkGetBeanProperty(ImageToolUtil.class); 172 173 return _imageTool; 174 } 175 176 public static boolean isNullOrDefaultSpacer(byte[] bytes) { 177 return getImageTool().isNullOrDefaultSpacer(bytes); 178 } 179 180 /** 181 * Detects the image format and creates an {@link 182 * com.liferay.portal.kernel.image.ImageBag} containing the {@link 183 * java.awt.image.RenderedImage} and image type. 184 * 185 * @param bytes the bytes to read 186 * @return the {@link com.liferay.portal.kernel.image.ImageBag} 187 * @throws IOException if an IO exception occurred 188 */ 189 public static ImageBag read(byte[] bytes) throws IOException { 190 return getImageTool().read(bytes); 191 } 192 193 /** 194 * Detects the image format and creates an {@link 195 * com.liferay.portal.kernel.image.ImageBag} containing the {@link 196 * java.awt.image.RenderedImage} and image type. 197 * 198 * @param file the file to read 199 * @return the {@link com.liferay.portal.kernel.image.ImageBag} 200 * @throws IOException if an IO exception occurred 201 */ 202 public static ImageBag read(File file) throws IOException { 203 return getImageTool().read(file); 204 } 205 206 public static ImageBag read(InputStream inputStream) throws IOException { 207 return getImageTool().read(inputStream); 208 } 209 210 /** 211 * Returns the scaled image based on the given width with the height 212 * calculated to preserve aspect ratio. 213 * 214 * @param renderedImage the image to scale 215 * @param width the new width; also used to calculate the new height 216 * @return the scaled image 217 */ 218 public static RenderedImage scale(RenderedImage renderedImage, int width) { 219 return getImageTool().scale(renderedImage, width); 220 } 221 222 /** 223 * Returns the scaled image based on the maximum height and width given 224 * while preserving the aspect ratio. If the image is already larger in both 225 * dimensions, the image will not be scaled. 226 * 227 * @param renderedImage the image to scale 228 * @param maxHeight the maximum height allowed for image 229 * @param maxWidth the maximum width allowed for image 230 * @return the scaled image 231 */ 232 public static RenderedImage scale( 233 RenderedImage renderedImage, int maxHeight, int maxWidth) { 234 235 return getImageTool().scale(renderedImage, maxHeight, maxWidth); 236 } 237 238 /** 239 * Encodes the image using the content or image type. 240 * 241 * @param renderedImage the image to encode 242 * @param contentType the content type (e.g., "image/jpeg") or image type 243 * (e.g., "jpg") to use during encoding 244 * @param os the stream to write to 245 * @throws IOException if an IO exception occurred 246 */ 247 public static void write( 248 RenderedImage renderedImage, String contentType, OutputStream os) 249 throws IOException { 250 251 getImageTool().write(renderedImage, contentType, os); 252 } 253 254 public void setImageTool(ImageTool imageTool) { 255 PortalRuntimePermission.checkSetBeanProperty(getClass()); 256 257 _imageTool = imageTool; 258 } 259 260 private static ImageTool _imageTool; 261 262 }