@@ -203,98 +203,4 @@ public static int BackgroundCompare(string rgba1, string rgba2)
203203 }
204204 return 0xFFFFFF ;
205205 }
206-
207- public static async Task < MemoryStream > CropAndResizeAsync ( Stream inStream , int width , int height , IImageFormat format )
208- {
209- var outStream = new MemoryStream ( ) ;
210- using ( var image = await Image . LoadAsync ( inStream ) )
211- {
212- var clone = image . Clone ( context => context
213- . Resize ( new ResizeOptions {
214- Mode = ResizeMode . Crop ,
215- Size = new Size ( width , height ) ,
216- } ) ) ;
217- await clone . SaveAsync ( outStream , format ) ;
218- }
219- outStream . Position = 0 ;
220- return outStream ;
221- }
222-
223- public static async Task ResizeArtifactsAsync ( this IVirtualFiles vfs , Artifact artifact )
224- {
225- var file = vfs . AssertFile ( artifact . FilePath ) ;
226- using var stream = file . OpenRead ( ) ;
227- using var image = await Image . LoadAsync ( stream ) ;
228- await ResizeArtifactsAsync ( vfs , artifact , image ) ;
229- }
230-
231- public static async Task ResizeArtifactsAsync ( this IVirtualFiles vfs , Artifact artifact , Image image , Microsoft . Extensions . Logging . ILogger ? log = null )
232- {
233- var sw = Stopwatch . StartNew ( ) ;
234- var tasks = new List < ( MemoryStream ms , Task task ) > ( ) ;
235- string basePath = artifact . FilePath . WithoutExtension ( ) ;
236-
237- // Small 118x118 or 118x207
238- if ( artifact . FilePathSmall == null )
239- {
240- var width = artifact . Width > artifact . Height
241- ? 207
242- : 118 ;
243- var height = artifact . Height > artifact . Width
244- ? 207
245- : 118 ;
246- using var clone = image . Clone ( context => context . Resize ( new ResizeOptions
247- {
248- Mode = ResizeMode . Crop ,
249- Size = new Size ( width , height ) ,
250- } ) ) ;
251- artifact . FilePathSmall = $ "{ basePath } _{ width } .webp";
252- var ms = MemoryStreamFactory . GetStream ( ) ;
253- await clone . SaveAsWebpAsync ( ms ) ;
254- tasks . Add ( ( ms , vfs . WriteFileAsync ( artifact . FilePathSmall , ms ) ) ) ;
255- }
256-
257- // Medium 288x288 or 288x504
258- if ( artifact . FilePathMedium == null )
259- {
260- var width = artifact . Width > artifact . Height
261- ? 504
262- : 288 ;
263- var height = artifact . Height > artifact . Width
264- ? 504
265- : 288 ;
266- using var clone = image . Clone ( context => context . Resize ( new ResizeOptions
267- {
268- Mode = ResizeMode . Crop ,
269- Size = new Size ( width , height ) ,
270- } ) ) ;
271-
272- artifact . FilePathMedium = $ "{ basePath } _{ width } .webp";
273- var ms = MemoryStreamFactory . GetStream ( ) ;
274- await clone . SaveAsWebpAsync ( ms ) ;
275- tasks . Add ( ( ms , vfs . WriteFileAsync ( artifact . FilePathMedium , ms ) ) ) ;
276- }
277-
278- // Large Original Size
279- if ( artifact . FilePathLarge == null )
280- {
281- artifact . FilePathLarge = $ "{ basePath } _{ artifact . Width } .webp";
282- var ms = MemoryStreamFactory . GetStream ( ) ;
283- await image . SaveAsWebpAsync ( ms ) ;
284- tasks . Add ( ( ms , vfs . WriteFileAsync ( artifact . FilePathLarge , ms ) ) ) ;
285- }
286-
287- // Run All Expensive I/O Ops concurrently
288- if ( tasks . Count > 0 )
289- {
290- log ? . LogInformation ( "Resize {Count} Artifacts: {ms}ms" , tasks . Count , sw . ElapsedMilliseconds ) ;
291-
292- sw . Restart ( ) ;
293-
294- await Task . WhenAll ( tasks . Select ( x => x . task ) ) ;
295- tasks . ForEach ( x => x . ms . Dispose ( ) ) ;
296-
297- log ? . LogInformation ( "Write {Count} Resized Artifacts: {ms}ms" , tasks . Count , sw . ElapsedMilliseconds ) ;
298- }
299- }
300206}
0 commit comments