@@ -105,9 +105,9 @@ public static void inputToOutputStream(InputStream in, OutputStream out) throws
105105 */
106106 public static boolean isImage (byte [] data ) {
107107 try {
108- ImageIO .read (new ByteArrayInputStream (data ));
108+ ImageIO .read (new ByteArrayInputStream (data )). getType () ;
109109 return true ;
110- } catch (IOException e ) {
110+ } catch (IOException | NullPointerException e ) {
111111 return false ;
112112 }
113113 }
@@ -121,18 +121,31 @@ public static boolean isImage(byte[] data) {
121121 * @throws IOException exception
122122 */
123123 public static BufferedImage reductionSize (BufferedImage image , long size ) throws IOException {
124- long lastSize = toByteArray (image , "png" ).length ;
124+ return reductionSize (image , size , "png" );
125+ }
126+
127+ /**
128+ * 画像を指定サイズまで縮小する
129+ *
130+ * @param image 対象画像
131+ * @param size サイズ
132+ * @param formatName フォーマット名
133+ * @return 縮小済み
134+ * @throws IOException exception
135+ */
136+ public static BufferedImage reductionSize (BufferedImage image , long size , String formatName ) throws IOException {
137+ long lastSize = toByteArray (image , formatName ).length ;
125138 if (lastSize <= size ) return image ;
126139 float scale = (float ) size / lastSize ;
127140 BufferedImage nimg = resize (image , (int ) (image .getWidth () * scale ), (int ) (image .getHeight () * scale ));
128- return reductionSizeW (nimg , size , lastSize );
141+ return reductionSizeW (nimg , size , lastSize , formatName );
129142 }
130143
131- private static BufferedImage reductionSizeW (BufferedImage image , long size , long lastSize ) throws IOException {
132- byte [] data = toByteArray (image , "png" );
144+ private static BufferedImage reductionSizeW (BufferedImage image , long size , long lastSize , String formatName ) throws IOException {
145+ byte [] data = toByteArray (image , formatName );
133146 if (data .length == lastSize ) throw new IOException ("Can't be smaller than this" );
134147 if (data .length <= size ) return image ;
135148 BufferedImage nimg = resize (image , image .getWidth () / 2 , image .getHeight () / 2 );
136- return reductionSizeW (nimg , size , data .length );
149+ return reductionSizeW (nimg , size , data .length , formatName );
137150 }
138151}
0 commit comments