Skip to content

Commit

Permalink
add ICompress interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dabutvin committed Sep 28, 2019
1 parent 7e175d9 commit 34683e0
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 30 deletions.
43 changes: 17 additions & 26 deletions CompressImagesFunction/CompressImages.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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 CompressImagesFunction.Compressors;
using ImageMagick;
using LibGit2Sharp;
using LibGit2Sharp.Handlers;
Expand All @@ -16,6 +16,12 @@ namespace CompressImagesFunction
{
public static class CompressImages
{
private static ICompress[] optimizers = new ICompress[]
{
new ImageMagickCompress(),
new SvgoCompress(),
};

public static bool Run(CompressimagesParameters parameters, ILogger logger)
{
CredentialsHandler credentialsProvider =
Expand Down Expand Up @@ -152,12 +158,6 @@ public static bool Run(CompressimagesParameters parameters, ILogger logger)
private static CompressionResult[] OptimizeImages(Repository repo, string localPath, string[] imagePaths, ILogger logger, bool aggressiveCompression)
{
var optimizedImages = new List<CompressionResult>();
ImageOptimizer imageOptimizer = new ImageOptimizer
{
OptimalCompression = true,
IgnoreUnsupportedFormats = true,
};

Parallel.ForEach(imagePaths, image =>
{
try
Expand All @@ -170,29 +170,20 @@ private static CompressionResult[] OptimizeImages(Repository repo, string localP
FileInfo file = new FileInfo(image);
double before = file.Length;
var extension = Path.GetExtension(image);
if (extension == ".svg")
foreach (var optimizer in optimizers)
{
var plugins = aggressiveCompression ? Svgo.LossyPlugins : Svgo.LosslessPlugins;
var processStartInfo = new ProcessStartInfo
if (optimizer.SupportedExtensions.Contains(extension))
{
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);
if (aggressiveCompression)
{
optimizer.LossyCompress(image);
}
else
{
optimizer.LosslessCompress(image);
}
}
}
else if (aggressiveCompression)
{
imageOptimizer.Compress(file);
}
else
{
imageOptimizer.LosslessCompress(file);
}
FileInfo fileAfter = new FileInfo(image);
if (fileAfter.Length < before)
Expand Down
11 changes: 11 additions & 0 deletions CompressImagesFunction/Compressors/ICompress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace CompressImagesFunction.Compressors
{
public interface ICompress
{
string[] SupportedExtensions { get; }

void LosslessCompress(string path);

void LossyCompress(string path);
}
}
30 changes: 30 additions & 0 deletions CompressImagesFunction/Compressors/ImageMagickCompress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using ImageMagick;

namespace CompressImagesFunction.Compressors
{
public class ImageMagickCompress : ICompress
{
private ImageOptimizer _imageOptimizer;

public ImageMagickCompress()
{
_imageOptimizer = new ImageOptimizer
{
OptimalCompression = true,
IgnoreUnsupportedFormats = true,
};
}

public string[] SupportedExtensions => new[] { ".png", ".jpg", ".jpeg", ".gif" };

public void LosslessCompress(string path)
{
_imageOptimizer.LosslessCompress(path);
}

public void LossyCompress(string path)
{
_imageOptimizer.Compress(path);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Diagnostics;
using System.Linq;

namespace CompressImagesFunction
namespace CompressImagesFunction.Compressors
{
public static class Svgo
public class SvgoCompress : ICompress
{
public static string[] LosslessPlugins => new[]
private static string[] losslessPlugins = new[]
{
"cleanupAttrs",
"cleanupListOfValues",
Expand All @@ -24,7 +25,7 @@ public static class Svgo
"sortAttrs",
};

public static string[] LossyPlugins => LosslessPlugins.Concat(new[]
private static string[] lossyPlugins = losslessPlugins.Concat(new[]
{
"cleanupEnableBackground",
"cleanupIDs",
Expand All @@ -50,5 +51,32 @@ public static class Svgo
"removeViewBox",
"removeXMLNS",
}).ToArray();

public string[] SupportedExtensions => new[] { ".svg" };

public void LosslessCompress(string path)
{
Compress(path, losslessPlugins);
}

public void LossyCompress(string path)
{
Compress(path, lossyPlugins);
}

private void Compress(string path, string[] plugins)
{
var processStartInfo = new ProcessStartInfo
{
UseShellExecute = false,
CreateNoWindow = true,
FileName = "svgo",
Arguments = $"{path} --config=\"{{\"\"full\"\":true}}\" --multipass --enable={string.Join(",", plugins)}"
};
using (var process = Process.Start(processStartInfo))
{
process.WaitForExit(10000);
}
}
}
}

0 comments on commit 34683e0

Please sign in to comment.