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.lang3.StringUtils; 024 025/** 026 * Class providing helper methods to resolve images. 027 */ 028public final class ImageResolverHelper 029{ 030 031 private ImageResolverHelper() 032 { 033 // Hides the default constructor. 034 } 035 036 /** 037 * Get an image's bytes encoded as base64, optionally resized. 038 * @param dataIs an input stream on the image bytes. 039 * @param mimeType the image mime type. 040 * @param height the specified height. Ignored if negative. 041 * @param width the specified width. Ignored if negative. 042 * @param maxHeight the maximum image height. Ignored if height or width is specified. 043 * @param maxWidth the maximum image width. Ignored if height or width is specified. 044 * @return the image bytes encoded as base64. 045 * @throws IOException if an error occurs. 046 */ 047 048 public static final String resolveImageAsBase64(InputStream dataIs, String mimeType, int height, int width, int maxHeight, int maxWidth) throws IOException 049 { 050 return resolveImageAsBase64(dataIs, mimeType, height, width, maxHeight, maxWidth, 0, 0); 051 } 052 053 /** 054 * Get an image's bytes encoded as base64, optionally resized. 055 * @param dataIs an input stream on the image bytes. 056 * @param mimeType the image mime type. 057 * @param height the specified height. Ignored if 0. 058 * @param width the specified width. Ignored if 0. 059 * @param maxHeight the maximum image height. Ignored if height or width is specified. 060 * @param maxWidth the maximum image width. Ignored if height or width is specified. 061 * @param cropHeight the cropping height. Ignored if 0. 062 * @param cropWidth the cropping width. Ignored if 0. 063 * @return the image bytes encoded as base64. 064 * @throws IOException if an error occurs. 065 */ 066 public static final String resolveImageAsBase64(InputStream dataIs, String mimeType, int height, int width, int maxHeight, int maxWidth, int cropHeight, int cropWidth) throws IOException 067 { 068 if (!StringUtils.startsWith(mimeType, "image/")) 069 { 070 return ""; 071 } 072 073 ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 074 075 String format = StringUtils.substringAfter(mimeType, "/"); 076 ImageHelper.generateThumbnail(dataIs, buffer, format, height, width, maxHeight, maxWidth, cropHeight, cropWidth); 077 078 String data = Base64.encodeBase64String(buffer.toByteArray()); 079 080 return "data:" + mimeType + ";base64," + data; 081 } 082}