001/* 002 * Copyright 2013 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.core.util; 017 018import java.io.ByteArrayOutputStream; 019import java.io.IOException; 020import java.io.InputStream; 021 022import org.apache.commons.codec.binary.Base64; 023import org.apache.commons.io.IOUtils; 024import org.apache.commons.lang.StringUtils; 025 026/** 027 * Class providing helper methods to resolve images. 028 */ 029public final class ImageResolverHelper 030{ 031 032 private ImageResolverHelper() 033 { 034 // Hides the default constructor. 035 } 036 037 /** 038 * Get an image's bytes encoded as base64, optionally resized. 039 * @param dataIs an input stream on the image bytes. 040 * @param mimeType the image mime type. 041 * @param height the specified height. Ignored if negative. 042 * @param width the specified width. Ignored if negative. 043 * @param maxHeight the maximum image height. Ignored if height or width is specified. 044 * @param maxWidth the maximum image width. Ignored if height or width is specified. 045 * @return the image bytes encoded as base64. 046 * @throws IOException if an error occurs. 047 */ 048 049 public static final String resolveImageAsBase64(InputStream dataIs, String mimeType, int height, int width, int maxHeight, int maxWidth) throws IOException 050 { 051 return resolveImageAsBase64(dataIs, mimeType, height, width, maxHeight, maxWidth, 0, 0); 052 } 053 054 /** 055 * Get an image's bytes encoded as base64, optionally resized. 056 * @param dataIs an input stream on the image bytes. 057 * @param mimeType the image mime type. 058 * @param height the specified height. Ignored if 0. 059 * @param width the specified width. Ignored if 0. 060 * @param maxHeight the maximum image height. Ignored if height or width is specified. 061 * @param maxWidth the maximum image width. Ignored if height or width is specified. 062 * @param cropHeight the cropping height. Ignored if 0. 063 * @param cropWidth the cropping width. Ignored if 0. 064 * @return the image bytes encoded as base64. 065 * @throws IOException if an error occurs. 066 */ 067 public static final String resolveImageAsBase64(InputStream dataIs, String mimeType, int height, int width, int maxHeight, int maxWidth, int cropHeight, int cropWidth) throws IOException 068 { 069 if (!StringUtils.startsWith(mimeType, "image/")) 070 { 071 return ""; 072 } 073 074 ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 075 076 if (height == 0 && width == 0 && maxHeight == 0 && maxWidth == 0 && cropHeight == 0 && cropWidth == 0) 077 { 078 IOUtils.copy(dataIs, buffer); 079 } 080 else 081 { 082 String format = StringUtils.substringAfter(mimeType, "/"); 083 ImageHelper.generateThumbnail(dataIs, buffer, format, height, width, maxHeight, maxWidth, cropHeight, cropWidth); 084 } 085 086 String data = Base64.encodeBase64String(buffer.toByteArray()); 087 088 return "data:" + mimeType + ";base64," + data; 089 } 090}