Skip to content

Commit 7606420

Browse files
committed
拆分出一个还不用使用 skia 的版本
依然有 21MB 大小
1 parent 81b93c8 commit 7606420

5 files changed

Lines changed: 168 additions & 149 deletions

File tree

src/MediaConverters/MediaConverters.Lib/Imaging/Optimizations/ImageFileOptimization.cs

Lines changed: 2 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010
using SixLabors.ImageSharp.PixelFormats;
1111
using SixLabors.ImageSharp.Processing;
1212

13-
using SkiaSharp;
14-
15-
using Svg.Skia;
16-
1713
namespace DotNetCampus.MediaConverters.Imaging.Optimizations;
1814

1915
public 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
}

src/MediaConverters/MediaConverters.Lib/Imaging/Optimizations/ImageFileOptimizationResult.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,13 @@ public static ImageFileOptimizationResult FailException(Exception e)
3939
FailureReason = ImageFileOptimizationFailureReason.Exception
4040
};
4141
}
42+
43+
public static ImageFileOptimizationResult NotSupported()
44+
{
45+
return new ImageFileOptimizationResult()
46+
{
47+
OptimizedImageFile = null,
48+
FailureReason = ImageFileOptimizationFailureReason.NotSupported
49+
};
50+
}
4251
}

src/MediaConverters/MediaConverters.Lib/MediaConverters.Lib.csproj

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@
1111

1212
<ItemGroup>
1313
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.10" />
14-
<PackageReference Include="SkiaSharp" Version="3.119.0" />
15-
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="3.119.0" />
16-
<PackageReference Include="Svg.Skia" Version="3.0.4" />
17-
<PackageReference Include="System.Drawing.Common" Version="9.0.7" />
18-
</ItemGroup>
19-
20-
<ItemGroup>
21-
<ProjectReference Include="..\SkiaWmfRenderer\src\SkiaWmfRenderer\SkiaWmfRenderer.csproj" />
2214
</ItemGroup>
2315

2416
<PropertyGroup>

src/MediaConverters/MediaConverters.Lib/Imaging/Optimizations/EnhancedGraphicsMetafileOptimization.cs renamed to src/MediaConverters/SkiaWmfRenderer/src/SkiaWmfRenderer/Optimizations/EnhancedGraphicsMetafileOptimization.cs

Lines changed: 153 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
using System;
1+
using DotNetCampus.MediaConverter.SkiaWmfRenderer;
2+
3+
using SkiaSharp;
4+
5+
using Svg.Skia;
6+
7+
using System;
28
using System.ComponentModel;
39
using System.Diagnostics;
410
using System.Drawing;
511
using System.Drawing.Imaging;
612
using System.IO;
713
using System.Runtime.InteropServices;
814
using System.Runtime.Versioning;
9-
10-
using DotNetCampus.MediaConverter.SkiaWmfRenderer;
15+
using System.Xml.Linq;
1116

1217
namespace DotNetCampus.MediaConverters.Imaging.Optimizations;
1318

@@ -349,4 +354,149 @@ private static ImageFileOptimizationResult ConvertInWindows(ImageFileOptimizatio
349354
};
350355
}
351356
}
357+
}
358+
359+
public static class SvgFileOptimization
360+
{
361+
/*
362+
if (IsExtension(".svg"))
363+
{
364+
// 如果是 svg 那就直接转换了,因为后续叠加特效等逻辑都不能支持 SVG 格式
365+
try
366+
{
367+
var outputFilePath = ConvertSvgToPngFile(context);
368+
if (outputFilePath is null)
369+
{
370+
return new ImageFileOptimizationResult()
371+
{
372+
OptimizedImageFile = null,
373+
FailureReason = ImageFileOptimizationFailureReason.NotSupported
374+
};
375+
}
376+
else
377+
{
378+
context.LogMessage($"Success ConvertSvgToPngFile. Update current image file to '{outputFilePath.FullName}'");
379+
context = context with
380+
{
381+
ImageFile = outputFilePath
382+
};
383+
}
384+
}
385+
catch (Exception e)
386+
{
387+
context.LogMessage($"Convert SVG to PNG failed: {e}");
388+
389+
return ImageFileOptimizationResult.FailException(e);
390+
}
391+
}
392+
else if (IsExtension(".wmf") ||
393+
IsExtension(".emf"))
394+
{
395+
var result = EnhancedGraphicsMetafileOptimization.ConvertWmfOrEmfToPngFile(context);
396+
if (result.OptimizedImageFile is not null)
397+
{
398+
context.LogMessage($"Success ConvertWmfOrEmfToPngFile. Update current image file to '{result.OptimizedImageFile}'");
399+
context = context with
400+
{
401+
ImageFile = result.OptimizedImageFile
402+
};
403+
}
404+
else
405+
{
406+
return result;
407+
}
408+
}
409+
*/
410+
411+
/// <summary>
412+
/// 转换 svg 文件为 png 文件
413+
/// </summary>
414+
/// <returns></returns>
415+
public static FileInfo? ConvertSvgToPngFile(ImageFileOptimizationContext context)
416+
{
417+
var imageFile = context.ImageFile;
418+
var workingFolder = context.WorkingFolder;
419+
420+
using var skSvg = new SKSvg();
421+
using var skPicture = skSvg.Load(imageFile.FullName);
422+
var outputFile = Path.Join(workingFolder.FullName,
423+
$"SVG_{Path.GetRandomFileName()}.png");
424+
var canSave = skSvg.Save(outputFile, SKColors.Transparent);
425+
if (canSave && File.Exists(outputFile))
426+
{
427+
return new FileInfo(outputFile);
428+
}
429+
430+
// 转换失败
431+
return null;
432+
}
433+
434+
public static async Task<FileInfo> FixSvgInvalidCharacterAsync(FileInfo svgFile,
435+
DirectoryInfo workingFolder)
436+
{
437+
using var fileStream = svgFile.OpenRead();
438+
using var streamReader = new StreamReader(fileStream);
439+
440+
var xDocument = await XDocument.LoadAsync(streamReader, LoadOptions.SetLineInfo, CancellationToken.None);
441+
bool anyUpdate = false;
442+
443+
foreach (var xElement in xDocument.Descendants("text"))
444+
{
445+
var value = xElement.Value;
446+
if (!string.IsNullOrEmpty(value) && value.Length > 0 && value[0] is var c && c == 0xFFFD)
447+
{
448+
// 0xFFFFD 是 utf8 特殊字符
449+
// 画出来就是�符号,不如删掉
450+
xElement.Value = string.Empty;
451+
452+
anyUpdate = true;
453+
}
454+
}
455+
456+
if (anyUpdate)
457+
{
458+
var convertedFile = Path.Join(workingFolder.FullName, $"FixSVG_{Path.GetRandomFileName()}.svg");
459+
using var stream = File.Create(convertedFile);
460+
await xDocument.SaveAsync(stream, SaveOptions.None, CancellationToken.None);
461+
return new FileInfo(convertedFile);
462+
}
463+
464+
// 啥都不用改,返回原图
465+
return svgFile;
466+
}
467+
468+
public static FileInfo FixSvgInvalidCharacter(ImageFileOptimizationContext context)
469+
{
470+
FileInfo svgFile = context.ImageFile;
471+
DirectoryInfo workingFolder = context.WorkingFolder;
472+
473+
using var fileStream = svgFile.OpenRead();
474+
using var streamReader = new StreamReader(fileStream);
475+
476+
var xDocument = XDocument.Load(streamReader, LoadOptions.SetLineInfo);
477+
bool anyUpdate = false;
478+
479+
foreach (var xElement in xDocument.Descendants("text"))
480+
{
481+
var value = xElement.Value;
482+
if (!string.IsNullOrEmpty(value) && value.Length > 0 && value[0] is var c && c == 0xFFFD)
483+
{
484+
// 0xFFFFD 是 utf8 特殊字符
485+
// 画出来就是�符号,不如删掉
486+
xElement.Value = string.Empty;
487+
488+
anyUpdate = true;
489+
}
490+
}
491+
492+
if (anyUpdate)
493+
{
494+
var convertedFile = Path.Join(workingFolder.FullName, $"FixSVG_{Path.GetRandomFileName()}.svg");
495+
xDocument.Save(convertedFile);
496+
return new FileInfo(convertedFile);
497+
}
498+
499+
// 啥都不用改,返回原图
500+
return svgFile;
501+
}
352502
}

src/MediaConverters/SkiaWmfRenderer/src/SkiaWmfRenderer/SkiaWmfRenderer.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
</PropertyGroup>
2626
<ItemGroup>
2727
<None Include="README.md" Pack="True" PackagePath="\" />
28-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" Condition="'$(GITHUB_ACTIONS)' == 'true'"/>
28+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" Condition="'$(GITHUB_ACTIONS)' == 'true'" />
2929
</ItemGroup>
3030

3131
<ItemGroup>
@@ -38,7 +38,9 @@
3838
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3939
</PackageReference>
4040
<PackageReference Include="SkiaSharp" Version="3.119.0" />
41-
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="3.119.0" />
41+
42+
<PackageReference Include="Svg.Skia" Version="3.0.4" />
43+
<PackageReference Include="System.Drawing.Common" Version="9.0.7" />
4244

4345
<PackageReference Include="HarfBuzzSharp" Version="8.3.1.1" />
4446
<PackageReference Include="HarfBuzzSharp.NativeAssets.Linux" Version="8.3.1.1" />

0 commit comments

Comments
 (0)