Skip to content

Commit

Permalink
Merge pull request #386 from dabutvin/svg-support
Browse files Browse the repository at this point in the history
Svg support
  • Loading branch information
dabutvin committed Sep 27, 2019
2 parents 7d52621 + 3f7f444 commit 46397c1
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Common/KnownImgPatterns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public static class KnownImgPatterns
{
public static readonly string[] ImgExtensions = { ".png", ".jpg", ".jpeg", ".gif", };
public static readonly string[] ImgExtensions = { ".png", ".jpg", ".jpeg", ".gif", ".svg" };
}
}
40 changes: 35 additions & 5 deletions CompressImagesFunction/CompressImages.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Common;
using Common.Messages;
using ImageMagick;
using LibGit2Sharp;
using LibGit2Sharp.Handlers;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
using Newtonsoft.Json;

namespace CompressImagesFunction
Expand Down Expand Up @@ -127,6 +125,12 @@ public static bool Run(CompressimagesParameters parameters, ILogger logger)
{
foreach (var image in optimizedImages)
{
if (image.OriginalPath.EndsWith(".svg"))
{
// do not use ImageMagick to verify SVGs
continue;
}

new MagickImage(image.OriginalPath).Dispose();
}
}
Expand Down Expand Up @@ -165,14 +169,40 @@ private static CompressionResult[] OptimizeImages(Repository repo, string localP
Console.WriteLine(image);
FileInfo file = new FileInfo(image);
double before = file.Length;
if (aggressiveCompression ? imageOptimizer.Compress(file) : imageOptimizer.LosslessCompress(file))
var extension = Path.GetExtension(image);
if (extension == ".svg")
{
var plugins = aggressiveCompression ? Svgo.LossyPlugins : Svgo.LosslessPlugins;
var processStartInfo = new ProcessStartInfo
{
UseShellExecute = false,
CreateNoWindow = true,
FileName = "svgo",
Arguments = $"{image} --config=\"{{\"\"full\"\":true}}\" --multipass --enable={string.Join(",", plugins)}"
};
using (var process = Process.Start(processStartInfo))
{
process.WaitForExit(10000);
}
}
else if (aggressiveCompression)
{
imageOptimizer.Compress(file);
}
else
{
imageOptimizer.LosslessCompress(file);
}
FileInfo fileAfter = new FileInfo(image);
if (fileAfter.Length < before)
{
optimizedImages.Add(new CompressionResult
{
Title = image.Substring(localPath.Length),
OriginalPath = image,
SizeBefore = before / 1024d,
SizeAfter = file.Length / 1024d,
SizeAfter = fileAfter.Length / 1024d,
});
}
},
Expand Down
54 changes: 54 additions & 0 deletions CompressImagesFunction/Svgo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Linq;

namespace CompressImagesFunction
{
public static class Svgo
{
public static string[] LosslessPlugins => new[]
{
"cleanupAttrs",
"cleanupListOfValues",
"cleanupNumericValues",
"convertColors",
"convertStyleToAttrs",
"minifyStyles",
"moveGroupAttrsToElems",
"removeComments",
"removeDoctype",
"removeEditorsNSData",
"removeEmptyAttrs",
"removeEmptyContainers",
"removeEmptyText",
"removeNonInheritableGroupAttrs",
"removeXMLProcInst",
"sortAttrs",
};

public static string[] LossyPlugins => LosslessPlugins.Concat(new[]
{
"cleanupEnableBackground",
"cleanupIDs",
"collapseGroups",
"convertPathData",
"convertShapeToPath",
"convertTransform",
"mergePaths",
"moveElemsAttrsToGroup",
"removeAttrs",
"removeDesc",
"removeDimensions",
"removeElementsByAttr",
"removeHiddenElems",
"removeMetadata",
"removeRasterImages",
"removeStyleElement",
"removeTitle",
"removeUnknownsAndDefaults",
"removeUnusedNS",
"removeUselessDefs",
"removeUselessStrokeAndFill",
"removeViewBox",
"removeXMLNS",
}).ToArray();
}
}
4 changes: 4 additions & 0 deletions Dockerfile.CompressImages
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ FROM mcr.microsoft.com/azure-functions/dotnet:2.0
RUN apt-get update && apt-get install -y --no-install-recommends --no-install-suggests \
curl libcurl3

RUN curl -sL https://deb.nodesource.com/setup_10.x | bash -
RUN apt-get install -y nodejs
RUN npm install -g svgo@^1.3.0

ENV AzureWebJobsScriptRoot=/home/site/wwwroot
COPY --from=dotnet ["/home/site/wwwroot", "/home/site/wwwroot"]

0 comments on commit 46397c1

Please sign in to comment.