1010using SixLabors . ImageSharp . PixelFormats ;
1111using SixLabors . ImageSharp . Processing ;
1212
13- using SkiaSharp ;
14-
15- using Svg . Skia ;
16-
1713namespace DotNetCampus . MediaConverters . Imaging . Optimizations ;
1814
1915public static class ImageFileOptimization
@@ -64,50 +60,12 @@ public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(Ima
6460
6561 if ( IsExtension ( ".svg" ) )
6662 {
67- // 如果是 svg 那就直接转换了,因为后续叠加特效等逻辑都不能支持 SVG 格式
68- try
69- {
70- var outputFilePath = ConvertSvgToPngFile ( context ) ;
71- if ( outputFilePath is null )
72- {
73- return new ImageFileOptimizationResult ( )
74- {
75- OptimizedImageFile = null ,
76- FailureReason = ImageFileOptimizationFailureReason . NotSupported
77- } ;
78- }
79- else
80- {
81- context . LogMessage ( $ "Success ConvertSvgToPngFile. Update current image file to '{ outputFilePath . FullName } '") ;
82- context = context with
83- {
84- ImageFile = outputFilePath
85- } ;
86- }
87- }
88- catch ( Exception e )
89- {
90- context . LogMessage ( $ "Convert SVG to PNG failed: { e } ") ;
91-
92- return ImageFileOptimizationResult . FailException ( e ) ;
93- }
63+ return ImageFileOptimizationResult . NotSupported ( ) ;
9464 }
9565 else if ( IsExtension ( ".wmf" ) ||
9666 IsExtension ( ".emf" ) )
9767 {
98- var result = EnhancedGraphicsMetafileOptimization . ConvertWmfOrEmfToPngFile ( context ) ;
99- if ( result . OptimizedImageFile is not null )
100- {
101- context . LogMessage ( $ "Success ConvertWmfOrEmfToPngFile. Update current image file to '{ result . OptimizedImageFile } '") ;
102- context = context with
103- {
104- ImageFile = result . OptimizedImageFile
105- } ;
106- }
107- else
108- {
109- return result ;
110- }
68+ return ImageFileOptimizationResult . NotSupported ( ) ;
11169 }
11270
11371 context . LogMessage ( $ "Start optimize image with ImageSharp. ImageFile: '{ context . ImageFile . FullName } '") ;
@@ -303,96 +261,4 @@ public static void LimitImageSize(Image<Rgba32> image, int? maxImageWidth, int?
303261 /// 图片压缩的最大高度
304262 /// </summary>
305263 private const int MaxHeight = 2160 ;
306-
307- /// <summary>
308- /// 转换 svg 文件为 png 文件
309- /// </summary>
310- /// <returns></returns>
311- public static FileInfo ? ConvertSvgToPngFile ( ImageFileOptimizationContext context )
312- {
313- var imageFile = context . ImageFile ;
314- var workingFolder = context . WorkingFolder ;
315-
316- using var skSvg = new SKSvg ( ) ;
317- using var skPicture = skSvg . Load ( imageFile . FullName ) ;
318- var outputFile = Path . Join ( workingFolder . FullName ,
319- $ "SVG_{ Path . GetRandomFileName ( ) } .png") ;
320- var canSave = skSvg . Save ( outputFile , SKColors . Transparent ) ;
321- if ( canSave && File . Exists ( outputFile ) )
322- {
323- return new FileInfo ( outputFile ) ;
324- }
325-
326- // 转换失败
327- return null ;
328- }
329-
330- public static async Task < FileInfo > FixSvgInvalidCharacterAsync ( FileInfo svgFile ,
331- DirectoryInfo workingFolder )
332- {
333- using var fileStream = svgFile . OpenRead ( ) ;
334- using var streamReader = new StreamReader ( fileStream ) ;
335-
336- var xDocument = await XDocument . LoadAsync ( streamReader , LoadOptions . SetLineInfo , CancellationToken . None ) ;
337- bool anyUpdate = false ;
338-
339- foreach ( var xElement in xDocument . Descendants ( "text" ) )
340- {
341- var value = xElement . Value ;
342- if ( ! string . IsNullOrEmpty ( value ) && value . Length > 0 && value [ 0 ] is var c && c == 0xFFFD )
343- {
344- // 0xFFFFD 是 utf8 特殊字符
345- // 画出来就是�符号,不如删掉
346- xElement . Value = string . Empty ;
347-
348- anyUpdate = true ;
349- }
350- }
351-
352- if ( anyUpdate )
353- {
354- var convertedFile = Path . Join ( workingFolder . FullName , $ "FixSVG_{ Path . GetRandomFileName ( ) } .svg") ;
355- using var stream = File . Create ( convertedFile ) ;
356- await xDocument . SaveAsync ( stream , SaveOptions . None , CancellationToken . None ) ;
357- return new FileInfo ( convertedFile ) ;
358- }
359-
360- // 啥都不用改,返回原图
361- return svgFile ;
362- }
363-
364- public static FileInfo FixSvgInvalidCharacter ( ImageFileOptimizationContext context )
365- {
366- FileInfo svgFile = context . ImageFile ;
367- DirectoryInfo workingFolder = context . WorkingFolder ;
368-
369- using var fileStream = svgFile . OpenRead ( ) ;
370- using var streamReader = new StreamReader ( fileStream ) ;
371-
372- var xDocument = XDocument . Load ( streamReader , LoadOptions . SetLineInfo ) ;
373- bool anyUpdate = false ;
374-
375- foreach ( var xElement in xDocument . Descendants ( "text" ) )
376- {
377- var value = xElement . Value ;
378- if ( ! string . IsNullOrEmpty ( value ) && value . Length > 0 && value [ 0 ] is var c && c == 0xFFFD )
379- {
380- // 0xFFFFD 是 utf8 特殊字符
381- // 画出来就是�符号,不如删掉
382- xElement . Value = string . Empty ;
383-
384- anyUpdate = true ;
385- }
386- }
387-
388- if ( anyUpdate )
389- {
390- var convertedFile = Path . Join ( workingFolder . FullName , $ "FixSVG_{ Path . GetRandomFileName ( ) } .svg") ;
391- xDocument . Save ( convertedFile ) ;
392- return new FileInfo ( convertedFile ) ;
393- }
394-
395- // 啥都不用改,返回原图
396- return svgFile ;
397- }
398264}
0 commit comments