55import java .awt .geom .AffineTransform ;
66import java .awt .image .AffineTransformOp ;
77import java .awt .image .BufferedImage ;
8- import java .io .File ;
9- import java .io .FileOutputStream ;
10- import java .io .IOException ;
11- import java .io .InputStream ;
8+ import java .io .*;
129
1310import com .genexus .util .GXFile ;
1411import org .apache .logging .log4j .Logger ;
1512
1613public class GxImageUtil {
1714 private static Logger log = org .apache .logging .log4j .LogManager .getLogger (GxImageUtil .class );
15+ private static int INVALID_CODE = -1 ;
1816
1917 private static String getImageAbsolutePath (String imageFile ){
18+ if (!isValidInput (imageFile ))
19+ return "" ;
20+
2021 if (CommonUtil .isUploadPrefix (imageFile )) {
2122 return new GXFile (imageFile ).getAbsolutePath ();
2223 }
@@ -27,80 +28,114 @@ private static String getImageAbsolutePath(String imageFile){
2728 private static InputStream getInputStream (String imageFile ) throws IOException {
2829 return new GXFile (imageFile ).getStream ();
2930 }
31+
3032 public static long getFileSize (String imageFile ){
33+ if (!isValidInput (imageFile ))
34+ return INVALID_CODE ;
3135
3236 return new GXFile (imageFile ).getLength ();
3337 }
3438
3539 public static int getImageHeight (String imageFile ) {
40+ if (!isValidInput (imageFile ))
41+ return INVALID_CODE ;
42+
3643 try (InputStream is = getInputStream (imageFile )) {
3744 return ImageIO .read (is ).getHeight ();
3845 }
3946 catch (Exception e ) {
4047 log .error ("getImageHeight " + imageFile + " failed" , e );
41- return 0 ;
4248 }
49+ return INVALID_CODE ;
50+ }
51+
52+ private static boolean isValidInput (String imageFile ) {
53+ boolean isValid = imageFile != null && imageFile .length () > 0 ;
54+ if (!isValid ) {
55+ log .debug ("Image Api - FileName cannot be empty" );
56+ }
57+ return isValid ;
4358 }
4459
4560 public static int getImageWidth (String imageFile ) {
61+ if (!isValidInput (imageFile ))
62+ return INVALID_CODE ;
63+
4664 try (InputStream is = getInputStream (imageFile )) {
4765 return ImageIO .read (is ).getWidth ();
4866 }
4967 catch (Exception e ) {
5068 log .error ("getImageWidth " + imageFile + " failed" , e );
51- return 0 ;
5269 }
70+ return INVALID_CODE ;
5371 }
5472
55- public static String crop (String imageFile , int x , int y , int width , int height ){
56- try {
57- String absolutePath = getImageAbsolutePath (imageFile );
58- BufferedImage image = ImageIO .read (new File (absolutePath ));
59- BufferedImage cropedImage = image .getSubimage (x , y , width , height );
60- ImageIO .write (cropedImage , CommonUtil .getFileType (absolutePath ), new FileOutputStream (absolutePath ));
73+ public static String crop (String imageFile , int x , int y , int width , int height ) {
74+ if (!isValidInput (imageFile ))
75+ return "" ;
76+
77+ try (InputStream is = getInputStream (imageFile )) {
78+ BufferedImage image = ImageIO .read (is );
79+ BufferedImage croppedImage = image .getSubimage (x , y , width , height );
80+ writeImage (croppedImage , imageFile );
6181 }
62- catch (IOException e ) {
82+ catch (Exception e ) {
6383 log .error ("crop " + imageFile + " failed" , e );
6484 }
6585 return imageFile ;
6686 }
6787
68- public static String flipHorizontally (String imageFile ){
69- try {
70- String absolutePath = getImageAbsolutePath (imageFile );
71- BufferedImage image = ImageIO .read (new File (absolutePath ));
88+ private static void writeImage (BufferedImage croppedImage , String destinationFilePathOrUrl ) throws IOException {
89+ try (ByteArrayOutputStream outStream = new ByteArrayOutputStream ()) {
90+ ImageIO .write (croppedImage , CommonUtil .getFileType (destinationFilePathOrUrl ), outStream );
91+ try (ByteArrayInputStream inStream = new ByteArrayInputStream (outStream .toByteArray ())) {
92+ new GXFile (destinationFilePathOrUrl ).create (inStream , true );
93+ }
94+ }
95+ }
96+
97+ public static String flipHorizontally (String imageFile ) {
98+ if (!isValidInput (imageFile ))
99+ return "" ;
100+
101+ try (InputStream is = getInputStream (imageFile )) {
102+ BufferedImage image = ImageIO .read (is );
72103 AffineTransform tx = AffineTransform .getScaleInstance (-1 , 1 );
73104 tx .translate (-image .getWidth (null ), 0 );
74105 AffineTransformOp op = new AffineTransformOp (tx , AffineTransformOp .TYPE_NEAREST_NEIGHBOR );
75- BufferedImage flipedImage = op .filter (image , null );
76- ImageIO . write ( flipedImage , CommonUtil . getFileType ( absolutePath ), new FileOutputStream ( absolutePath ) );
106+ BufferedImage flippedImage = op .filter (image , null );
107+ writeImage ( flippedImage , imageFile );
77108 }
78- catch (IOException e ) {
109+ catch (Exception e ) {
79110 log .error ("flip horizontal " + imageFile + " failed" , e );
80111 }
81112 return imageFile ;
82113 }
83114
84- public static String flipVertically (String imageFile ){
85- try {
86- String absolutePath = getImageAbsolutePath (imageFile );
87- BufferedImage image = ImageIO .read (new File (absolutePath ));
115+ public static String flipVertically (String imageFile ) {
116+ if (!isValidInput (imageFile ))
117+ return "" ;
118+
119+ try (InputStream is = getInputStream (imageFile )) {
120+ BufferedImage image = ImageIO .read (is );
88121 AffineTransform tx = AffineTransform .getScaleInstance (1 , -1 );
89122 tx .translate (0 , -image .getHeight (null ));
90123 AffineTransformOp op = new AffineTransformOp (tx , AffineTransformOp .TYPE_NEAREST_NEIGHBOR );
91- BufferedImage flipedImage = op .filter (image , null );
92- ImageIO . write ( flipedImage , CommonUtil . getFileType ( absolutePath ), new FileOutputStream ( absolutePath ) );
124+ BufferedImage flippedImage = op .filter (image , null );
125+ writeImage ( flippedImage , imageFile );
93126 }
94- catch (IOException e ) {
127+ catch (Exception e ) {
95128 log .error ("flip vertical " + imageFile + " failed" , e );
96129 }
97130 return imageFile ;
98131 }
99132
100- public static String resize (String imageFile , int width , int height , boolean keepAspectRatio ){
101- try {
102- String absolutePath = getImageAbsolutePath (imageFile );
103- BufferedImage image = ImageIO .read (new File (absolutePath ));
133+ public static String resize (String imageFile , int width , int height , boolean keepAspectRatio ) {
134+ if (!isValidInput (imageFile ))
135+ return "" ;
136+
137+ try (InputStream is = getInputStream (imageFile )) {
138+ BufferedImage image = ImageIO .read (is );
104139 if (keepAspectRatio ) {
105140 double imageHeight = image .getHeight ();
106141 double imageWidth = image .getWidth ();
@@ -115,34 +150,37 @@ public static String resize(String imageFile, int width, int height, boolean kee
115150 Graphics2D g2d = resizedImage .createGraphics ();
116151 g2d .drawImage (image , 0 , 0 , width , height , null );
117152 g2d .dispose ();
118- ImageIO . write (resizedImage , CommonUtil . getFileType ( absolutePath ), new FileOutputStream ( absolutePath ) );
153+ writeImage (resizedImage , imageFile );
119154 }
120- catch (IOException e ) {
155+ catch (Exception e ) {
121156 log .error ("resize " + imageFile + " failed" , e );
122157 }
123158 return imageFile ;
124159 }
125160
126- public static String scale (String imageFile , short percent ){
127- try {
128- String absolutePath = getImageAbsolutePath (imageFile );
129- BufferedImage image = ImageIO .read (new File (absolutePath ));
161+ public static String scale (String imageFile , short percent ) {
162+ if (!isValidInput (imageFile ))
163+ return "" ;
164+
165+ try (InputStream is = getInputStream (imageFile )) {
166+ BufferedImage image = ImageIO .read (is );
130167 imageFile = resize (imageFile , image .getWidth () * percent / 100 , image .getHeight () * percent / 100 ,true );
131168 }
132- catch (IOException e ) {
169+ catch (Exception e ) {
133170 log .error ("scale " + imageFile + " failed" , e );
134171 }
135172 return imageFile ;
136173 }
137174
138- public static String rotate (String imageFile , short angle ){
139- try {
140- String absolutePath = getImageAbsolutePath (imageFile );
141- BufferedImage image = ImageIO .read (new File (absolutePath ));
175+ public static String rotate (String imageFile , short angle ) {
176+ if (!isValidInput (imageFile ))
177+ return "" ;
178+ try (InputStream is = getInputStream (imageFile )) {
179+ BufferedImage image = ImageIO .read (is );
142180 BufferedImage rotatedImage = rotateImage (image , angle );
143- ImageIO . write (rotatedImage , CommonUtil . getFileType ( absolutePath ), new FileOutputStream ( absolutePath ) );
181+ writeImage (rotatedImage , imageFile );
144182 }
145- catch (IOException e ) {
183+ catch (Exception e ) {
146184 log .error ("rotate " + imageFile + " failed" , e );
147185 }
148186 return imageFile ;
0 commit comments