44// </copyright>
55// -----------------------------------------------------------------------
66
7+ using Microsoft . AspNetCore . Mvc ;
78using Pixsys . Library . Common . Extensions ;
89using Pixsys . Library . Media . Common . Enums ;
910using Pixsys . Library . Media . Common . Extensions ;
1011using Pixsys . Library . Media . Common . Helpers ;
1112using Pixsys . Library . Media . Common . Models ;
13+ using Pixsys . Library . Media . ImageManager . Constants ;
1214using Pixsys . Library . Media . ImageManager . Interfaces ;
1315using Pixsys . Library . Media . ImageManager . Models ;
16+ using SixLabors . ImageSharp . Formats . Webp ;
1417using SixLabors . ImageSharp . Processing ;
1518using System . Text ;
1619
@@ -66,153 +69,146 @@ public IImageManagerOperations Resize(ResizeOptions options)
6669 /// <inheritdoc />
6770 public IImageManagerOperations BlackWhite ( )
6871 {
69- return AddAction ( x => x . BlackWhite ( ) , "_bw" ) ;
72+ return AddAction ( x => x . BlackWhite ( ) , ImageManagerOperationSuffixes . BlackWhite ) ;
7073 }
7174
7275 /// <inheritdoc />
7376 public IImageManagerOperations GaussianBlur ( )
7477 {
75- return AddAction ( x => x . GaussianBlur ( ) , "_gb" ) ;
78+ return AddAction ( x => x . GaussianBlur ( ) , ImageManagerOperationSuffixes . GaussianBlur ) ;
7679 }
7780
7881 /// <inheritdoc />
7982 public IImageManagerOperations Kodachrome ( )
8083 {
81- return AddAction ( x => x . Kodachrome ( ) , "_kc" ) ;
84+ return AddAction ( x => x . Kodachrome ( ) , ImageManagerOperationSuffixes . Kodachrome ) ;
8285 }
8386
8487 /// <inheritdoc />
8588 public IImageManagerOperations AutoOrient ( )
8689 {
87- return AddAction ( x => x . AutoOrient ( ) , "_ao" ) ;
90+ return AddAction ( x => x . AutoOrient ( ) , ImageManagerOperationSuffixes . AutoOrient ) ;
8891 }
8992
9093 /// <inheritdoc />
9194 public IImageManagerOperations BokehBlur ( )
9295 {
93- return AddAction ( x => x . BokehBlur ( ) , "_bkb" ) ;
96+ return AddAction ( x => x . BokehBlur ( ) , ImageManagerOperationSuffixes . BokehBlur ) ;
9497 }
9598
9699 /// <inheritdoc />
97100 public IImageManagerOperations BoxBlur ( )
98101 {
99- return AddAction ( x => x . BoxBlur ( ) , "_bxb" ) ;
102+ return AddAction ( x => x . BoxBlur ( ) , ImageManagerOperationSuffixes . BoxBlur ) ;
100103 }
101104
102105 /// <inheritdoc />
103106 public IImageManagerOperations Brightness ( float amount )
104107 {
105- return AddAction ( x => x . Brightness ( amount ) , $ "_br { amount } ") ;
108+ return AddAction ( x => x . Brightness ( amount ) , $ "{ ImageManagerOperationSuffixes . Brightness } { amount } ") ;
106109 }
107110
108111 /// <inheritdoc />
109112 public IImageManagerOperations Crop ( int width , int height )
110113 {
111- return AddAction ( x => x . Crop ( width , height ) , $ "_cr { width } _{ height } ") ;
114+ return AddAction ( x => x . Crop ( width , height ) , $ "{ ImageManagerOperationSuffixes . Crop } { width } _{ height } ") ;
112115 }
113116
114117 /// <inheritdoc />
115118 public IImageManagerOperations Flip ( FlipMode mode )
116119 {
117- return AddAction ( x => x . Flip ( mode ) , "_fl" ) ;
120+ return AddAction ( x => x . Flip ( mode ) , ImageManagerOperationSuffixes . Flip ) ;
118121 }
119122
120123 /// <inheritdoc />
121124 public IImageManagerOperations Glow ( )
122125 {
123- return AddAction ( x => x . Glow ( ) , "_gl" ) ;
126+ return AddAction ( x => x . Glow ( ) , ImageManagerOperationSuffixes . Glow ) ;
124127 }
125128
126129 /// <inheritdoc />
127130 public IImageManagerOperations Grayscale ( )
128131 {
129- return AddAction ( x => x . Grayscale ( ) , "_gs" ) ;
132+ return AddAction ( x => x . Grayscale ( ) , ImageManagerOperationSuffixes . Grayscale ) ;
130133 }
131134
132135 /// <inheritdoc />
133136 public IImageManagerOperations Hue ( float degrees )
134137 {
135- return AddAction ( x => x . Hue ( degrees ) , $ "_hu { degrees } ") ;
138+ return AddAction ( x => x . Hue ( degrees ) , $ "{ ImageManagerOperationSuffixes . Hue } { degrees } ") ;
136139 }
137140
138141 /// <inheritdoc />
139142 public IImageManagerOperations Invert ( )
140143 {
141- return AddAction ( x => x . Invert ( ) , "_inv" ) ;
144+ return AddAction ( x => x . Invert ( ) , ImageManagerOperationSuffixes . Invert ) ;
142145 }
143146
144147 /// <inheritdoc />
145148 public IImageManagerOperations Lightness ( float amount )
146149 {
147- return AddAction ( x => x . Lightness ( amount ) , $ "_lig { amount } ") ;
150+ return AddAction ( x => x . Lightness ( amount ) , $ "{ ImageManagerOperationSuffixes . Lightness } { amount } ") ;
148151 }
149152
150153 /// <inheritdoc />
151154 public IImageManagerOperations OilPaint ( )
152155 {
153- return AddAction ( x => x . OilPaint ( ) , "_oil" ) ;
156+ return AddAction ( x => x . OilPaint ( ) , ImageManagerOperationSuffixes . OilPaint ) ;
154157 }
155158
156159 /// <inheritdoc />
157160 public IImageManagerOperations Opacity ( float amount )
158161 {
159- return AddAction ( x => x . Opacity ( amount ) , $ "_opa { amount } ") ;
162+ return AddAction ( x => x . Opacity ( amount ) , $ "{ ImageManagerOperationSuffixes . Opacity } { amount } ") ;
160163 }
161164
162165 /// <inheritdoc />
163166 public IImageManagerOperations Pixelate ( )
164167 {
165- return AddAction ( x => x . Pixelate ( ) , "_pxl" ) ;
168+ return AddAction ( x => x . Pixelate ( ) , ImageManagerOperationSuffixes . Pixelate ) ;
166169 }
167170
168171 /// <inheritdoc />
169172 public IImageManagerOperations Polaroid ( )
170173 {
171- return AddAction ( x => x . Polaroid ( ) , "_pol" ) ;
174+ return AddAction ( x => x . Polaroid ( ) , ImageManagerOperationSuffixes . Polaroid ) ;
172175 }
173176
174177 /// <inheritdoc />
175178 public IImageManagerOperations Saturate ( float amount )
176179 {
177- return AddAction ( x => x . Saturate ( amount ) , $ "_sat { amount } ") ;
180+ return AddAction ( x => x . Saturate ( amount ) , $ "{ ImageManagerOperationSuffixes . Saturate } { amount } ") ;
178181 }
179182
180183 /// <inheritdoc />
181184 public IImageManagerOperations Sepia ( )
182185 {
183- return AddAction ( x => x . Sepia ( ) , "_sep" ) ;
186+ return AddAction ( x => x . Sepia ( ) , ImageManagerOperationSuffixes . Sepia ) ;
184187 }
185188
186189 /// <inheritdoc />
187190 public IImageManagerOperations Vignette ( )
188191 {
189- return AddAction ( x => x . Vignette ( ) , "_vig" ) ;
192+ return AddAction ( x => x . Vignette ( ) , ImageManagerOperationSuffixes . Vignette ) ;
190193 }
191194
192195 /// <inheritdoc />
193- public async Task < ImageProperties > SaveAsync ( DirectoryInfo folder , string fileName , ImageFormat format , bool addActionsSuffixes = false , string profileName = "" )
196+ public async Task < FileContentResult > SaveStreamAsync ( )
194197 {
195198 string sourceImagePath = Path . Combine ( sourceFolder . FullName , sourceFileName ) ;
196- using SixLabors . ImageSharp . Image image = SixLabors . ImageSharp . Image . Load ( sourceImagePath ) ;
197- void CombinedAction ( IImageProcessingContext context )
198- {
199- foreach ( Action < IImageProcessingContext > action in actions )
200- {
201- action ( context ) ;
202- }
203- }
204-
205- if ( actions . Count != 0 )
206- {
207- image . Mutate ( CombinedAction ) ;
208- }
209-
210- string ? suffix = null ;
211- if ( addActionsSuffixes )
212- {
213- suffix = imagesuffix . ToString ( ) ;
214- }
199+ using SixLabors . ImageSharp . Image sourceImage = await SixLabors . ImageSharp . Image . LoadAsync ( sourceImagePath ) ;
200+ ( SixLabors . ImageSharp . Image image , _ ) = ApplyTransformations ( sourceImage , false ) ;
201+ await using MemoryStream outStream = new ( ) ;
202+ await image . SaveAsync ( outStream , new WebpEncoder ( ) ) ;
203+ return new FileContentResult ( outStream . ToArray ( ) , "image/webp" ) ;
204+ }
215205
206+ /// <inheritdoc />
207+ public async Task < ImageProperties > SaveImageAsync ( DirectoryInfo folder , string fileName , ImageFormat format , bool addActionsSuffixes = false , string profileName = "" )
208+ {
209+ string sourceImagePath = Path . Combine ( sourceFolder . FullName , sourceFileName ) ;
210+ using SixLabors . ImageSharp . Image sourceImage = await SixLabors . ImageSharp . Image . LoadAsync ( sourceImagePath ) ;
211+ ( SixLabors . ImageSharp . Image image , string ? suffix ) = ApplyTransformations ( sourceImage , addActionsSuffixes ) ;
216212 ImageProperties imageOutput = await image . SaveImageAsync ( folder , fileName , suffix ?? string . Empty , format ) ;
217213
218214 // Remove original or keep the more lightweight image
@@ -227,6 +223,7 @@ void CombinedAction(IImageProcessingContext context)
227223 }
228224 }
229225
226+ // Thumbnails Creation
230227 if ( ! string . IsNullOrWhiteSpace ( profileName ) )
231228 {
232229 ImageProfile profile = GetProfile ( profileName ) ;
@@ -281,5 +278,35 @@ private ImageManagerOperations AddAction(Action<IImageProcessingContext> action,
281278 {
282279 return new ImageManagerOperations ( settings , sourceFolder , sourceFileName , new List < Action < IImageProcessingContext > > ( actions ) { action } , imagesuffix . Copy ( ) . Append ( suffix ) ) ;
283280 }
281+
282+ /// <summary>
283+ /// Applies image transformations.
284+ /// </summary>
285+ /// <param name="image">The source image.</param>
286+ /// <param name="addActionsSuffixes">A value indicating wether the action suffixes must be added to the file name or not.</param>
287+ /// <returns>The <see cref="SixLabors.ImageSharp.Image "/>.</returns>
288+ private ( SixLabors . ImageSharp . Image Image , string ? Suffix ) ApplyTransformations ( SixLabors . ImageSharp . Image image , bool addActionsSuffixes = false )
289+ {
290+ void CombinedAction ( IImageProcessingContext context )
291+ {
292+ foreach ( Action < IImageProcessingContext > action in actions )
293+ {
294+ action ( context ) ;
295+ }
296+ }
297+
298+ if ( actions . Count != 0 )
299+ {
300+ image . Mutate ( CombinedAction ) ;
301+ }
302+
303+ string ? suffix = null ;
304+ if ( addActionsSuffixes )
305+ {
306+ suffix = imagesuffix . ToString ( ) ;
307+ }
308+
309+ return ( image , suffix ) ;
310+ }
284311 }
285312}
0 commit comments