Skip to content

Commit

Permalink
Started adding MozJpeg compression
Browse files Browse the repository at this point in the history
  • Loading branch information
ElSiipo committed Oct 5, 2019
1 parent a6da825 commit fa8ead2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CompressImagesFunction/CompressImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static class CompressImages
{
new ImageMagickCompress(),
new SvgoCompress(),
new MozJpegCompress(),
};

public static bool Run(CompressimagesParameters parameters, ILogger logger)
Expand Down Expand Up @@ -177,6 +178,7 @@ private static CompressionResult[] OptimizeImages(Repository repo, string localP
if (aggressiveCompression)
{
optimizer.LossyCompress(image);
optimizer.LosslessCompress(image);
}
else
{
Expand Down
39 changes: 39 additions & 0 deletions CompressImagesFunction/Compressors/MozJpegCompress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Diagnostics;
using System.Linq;

namespace CompressImagesFunction.Compressors
{
public class MozJpegCompress : ICompress
{
private static string[] losslessPlugins = new[] { "jpegtran", };
private static string[] lossyPlugins = losslessPlugins.Concat(new[] { "cjpeg", }).ToArray();

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

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 = "mozjpeg",
Arguments = $"{path} --config=\"{{\"\"full\"\":true}}\" -quality --enable={string.Join(",", plugins)}"
};
using (var process = Process.Start(processStartInfo))
{
process.WaitForExit(10000);
}
}
}
}
1 change: 1 addition & 0 deletions Dockerfile.CompressImages
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends --no-install-su
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
RUN npm install mozjpeg

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

0 comments on commit fa8ead2

Please sign in to comment.