From fa8ead200d31590271e3042250afe5549e4ff4dd Mon Sep 17 00:00:00 2001 From: Christian Bartelius Date: Sat, 5 Oct 2019 23:28:31 +0200 Subject: [PATCH 1/6] Started adding MozJpeg compression --- CompressImagesFunction/CompressImages.cs | 2 + .../Compressors/MozJpegCompress.cs | 39 +++++++++++++++++++ Dockerfile.CompressImages | 1 + 3 files changed, 42 insertions(+) create mode 100644 CompressImagesFunction/Compressors/MozJpegCompress.cs diff --git a/CompressImagesFunction/CompressImages.cs b/CompressImagesFunction/CompressImages.cs index 5e28459ac..a9e266ad8 100644 --- a/CompressImagesFunction/CompressImages.cs +++ b/CompressImagesFunction/CompressImages.cs @@ -20,6 +20,7 @@ public static class CompressImages { new ImageMagickCompress(), new SvgoCompress(), + new MozJpegCompress(), }; public static bool Run(CompressimagesParameters parameters, ILogger logger) @@ -177,6 +178,7 @@ private static CompressionResult[] OptimizeImages(Repository repo, string localP if (aggressiveCompression) { optimizer.LossyCompress(image); + optimizer.LosslessCompress(image); } else { diff --git a/CompressImagesFunction/Compressors/MozJpegCompress.cs b/CompressImagesFunction/Compressors/MozJpegCompress.cs new file mode 100644 index 000000000..15c17dd82 --- /dev/null +++ b/CompressImagesFunction/Compressors/MozJpegCompress.cs @@ -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); + } + } + } +} diff --git a/Dockerfile.CompressImages b/Dockerfile.CompressImages index 2134179b3..1a4210874 100644 --- a/Dockerfile.CompressImages +++ b/Dockerfile.CompressImages @@ -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"] From 25ead99ee51a418cba39b93f63f26936d3b9c780 Mon Sep 17 00:00:00 2001 From: Christian Bartelius Date: Sun, 6 Oct 2019 13:30:49 +0200 Subject: [PATCH 2/6] Updated arguments --- .../Compressors/MozJpegCompress.cs | 32 +++++++++---------- Test/MozJpegCompressTests.cs | 26 +++++++++++++++ 2 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 Test/MozJpegCompressTests.cs diff --git a/CompressImagesFunction/Compressors/MozJpegCompress.cs b/CompressImagesFunction/Compressors/MozJpegCompress.cs index 15c17dd82..40d02a3f1 100644 --- a/CompressImagesFunction/Compressors/MozJpegCompress.cs +++ b/CompressImagesFunction/Compressors/MozJpegCompress.cs @@ -1,34 +1,32 @@ -using System; -using System.Diagnostics; -using System.Linq; +using System.Diagnostics; namespace CompressImagesFunction.Compressors { public class MozJpegCompress : ICompress { - private static string[] losslessPlugins = new[] { "jpegtran", }; - private static string[] lossyPlugins = losslessPlugins.Concat(new[] { "cjpeg", }).ToArray(); + private static readonly string LosslessPlugin = "mozjpegtran"; + private static readonly string LossyPlugin = "mozcjpeg"; - public string[] SupportedExtensions => new[] { ".jpg", ".jpeg" }; + public string[] SupportedExtensions => + new[] { ".jpg", ".jpeg" }; - public void LosslessCompress(string path) - { - Compress(path, losslessPlugins); - } + public void LosslessCompress(string path) => + Compress(path, LosslessPlugin); - public void LossyCompress(string path) - { - Compress(path, lossyPlugins); - } + public void LossyCompress(string path) => + Compress(path, LossyPlugin); - private void Compress(string path, string[] plugins) + private void Compress(string path, string compressionType) { var processStartInfo = new ProcessStartInfo { UseShellExecute = false, CreateNoWindow = true, - FileName = "mozjpeg", - Arguments = $"{path} --config=\"{{\"\"full\"\":true}}\" -quality --enable={string.Join(",", plugins)}" + FileName = compressionType, + Arguments = $"-quality 80 -outfile {path} {path}" + + // FileName = "mozjpeg", + // Arguments = $"{compressionType} -quality 80 -outfile {path} {path}" }; using (var process = Process.Start(processStartInfo)) { diff --git a/Test/MozJpegCompressTests.cs b/Test/MozJpegCompressTests.cs new file mode 100644 index 000000000..a5c221f93 --- /dev/null +++ b/Test/MozJpegCompressTests.cs @@ -0,0 +1,26 @@ +using System.IO; +using System.Linq; +using Common; +using CompressImagesFunction; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Test +{ + [TestClass] + public class MozJpegCompressTests + { + [TestMethod] + public void TestPath() + { + var image = ImageQuery.FindImages("data", new RepoConfiguration()).First(); + + FileInfo file = new FileInfo(image); + double before = file.Length / 1024d; + + new CompressImagesFunction.Compressors.MozJpegCompress().LosslessCompress(image); + FileInfo fileAfter = new FileInfo(image); + + Assert.IsTrue(before > fileAfter.Length/1024d); + } + } +} From 4b8debec3ccd48c61012b1dd7c37c9c77e00e71e Mon Sep 17 00:00:00 2001 From: Christian Bartelius Date: Sun, 6 Oct 2019 13:44:42 +0200 Subject: [PATCH 3/6] Removed quality variable from Lossless compression --- .../Compressors/MozJpegCompress.cs | 6 ++--- Test/MozJpegCompressTests.cs | 26 ------------------- 2 files changed, 3 insertions(+), 29 deletions(-) delete mode 100644 Test/MozJpegCompressTests.cs diff --git a/CompressImagesFunction/Compressors/MozJpegCompress.cs b/CompressImagesFunction/Compressors/MozJpegCompress.cs index 40d02a3f1..21e25242b 100644 --- a/CompressImagesFunction/Compressors/MozJpegCompress.cs +++ b/CompressImagesFunction/Compressors/MozJpegCompress.cs @@ -14,16 +14,16 @@ public void LosslessCompress(string path) => Compress(path, LosslessPlugin); public void LossyCompress(string path) => - Compress(path, LossyPlugin); + Compress(path, LossyPlugin, "-quality 80 "); - private void Compress(string path, string compressionType) + private void Compress(string path, string compressionType, string switches = "") { var processStartInfo = new ProcessStartInfo { UseShellExecute = false, CreateNoWindow = true, FileName = compressionType, - Arguments = $"-quality 80 -outfile {path} {path}" + Arguments = $"{switches}-outfile {path} {path}" // FileName = "mozjpeg", // Arguments = $"{compressionType} -quality 80 -outfile {path} {path}" diff --git a/Test/MozJpegCompressTests.cs b/Test/MozJpegCompressTests.cs deleted file mode 100644 index a5c221f93..000000000 --- a/Test/MozJpegCompressTests.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System.IO; -using System.Linq; -using Common; -using CompressImagesFunction; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace Test -{ - [TestClass] - public class MozJpegCompressTests - { - [TestMethod] - public void TestPath() - { - var image = ImageQuery.FindImages("data", new RepoConfiguration()).First(); - - FileInfo file = new FileInfo(image); - double before = file.Length / 1024d; - - new CompressImagesFunction.Compressors.MozJpegCompress().LosslessCompress(image); - FileInfo fileAfter = new FileInfo(image); - - Assert.IsTrue(before > fileAfter.Length/1024d); - } - } -} From 13df33ceea52fd5e1f4d839f75aadf26c736ea98 Mon Sep 17 00:00:00 2001 From: Christian Bartelius Date: Sun, 6 Oct 2019 13:49:51 +0200 Subject: [PATCH 4/6] Updated docker file --- Dockerfile.CompressImages | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile.CompressImages b/Dockerfile.CompressImages index 1a4210874..433bfaf13 100644 --- a/Dockerfile.CompressImages +++ b/Dockerfile.CompressImages @@ -16,7 +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 +RUN npm install -g mozjpeg ENV AzureWebJobsScriptRoot=/home/site/wwwroot COPY --from=dotnet ["/home/site/wwwroot", "/home/site/wwwroot"] From 2a017bd384c3ca1605251f1e7fc809e1342a05e2 Mon Sep 17 00:00:00 2001 From: Christian Bartelius Date: Mon, 7 Oct 2019 21:03:25 +0200 Subject: [PATCH 5/6] Updated dockerfile --- Dockerfile.CompressImages | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Dockerfile.CompressImages b/Dockerfile.CompressImages index 433bfaf13..911ef7506 100644 --- a/Dockerfile.CompressImages +++ b/Dockerfile.CompressImages @@ -11,12 +11,21 @@ RUN cd /src/CompressImagesFunction && cp bin/Release/netstandard2.0/bin/runtimes 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 + curl libcurl3 autoconf automake libtool nasm make wget pkg-config 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 -g mozjpeg + +# Add support for mozjpeg +RUN cd /tmp && wget https://github.com/mozilla/mozjpeg/archive/v3.3.1.tar.gz +RUN cd /tmp && tar -xzf v3.3.1.tar.gz +RUN cd /tmp/mozjpeg-3.3.1 && autoreconf -fiv +RUN mkdir /tmp/mozjpeg-3.3.1/build +RUN cd /tmp/mozjpeg-3.3.1/build && sh ../configure && make install + +RUN ln -s /opt/mozjpeg/bin/jpegtran /usr/local/bin/mozjpegtran +RUN ln -s /opt/mozjpeg/bin/cjpeg /usr/local/bin/mozcjpeg ENV AzureWebJobsScriptRoot=/home/site/wwwroot COPY --from=dotnet ["/home/site/wwwroot", "/home/site/wwwroot"] From 51955bdd19d87f316c8d31f596dfd4e9071c3ec2 Mon Sep 17 00:00:00 2001 From: Christian Bartelius Date: Thu, 10 Oct 2019 08:23:16 +0200 Subject: [PATCH 6/6] Added temp path, removing original file, rename temp to original. --- .../Compressors/MozJpegCompress.cs | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/CompressImagesFunction/Compressors/MozJpegCompress.cs b/CompressImagesFunction/Compressors/MozJpegCompress.cs index 21e25242b..a556a941c 100644 --- a/CompressImagesFunction/Compressors/MozJpegCompress.cs +++ b/CompressImagesFunction/Compressors/MozJpegCompress.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.IO; namespace CompressImagesFunction.Compressors { @@ -10,23 +11,31 @@ public class MozJpegCompress : ICompress public string[] SupportedExtensions => new[] { ".jpg", ".jpeg" }; - public void LosslessCompress(string path) => - Compress(path, LosslessPlugin); + public void LosslessCompress(string path) + { + var arguments = $"-outfile {path}"; + Compress(LosslessPlugin, arguments); + } + + public void LossyCompress(string path) + { + var tempPath = path + ".tmp"; + var arguments = $"-quality 80 -outfile {tempPath} {path}"; - public void LossyCompress(string path) => - Compress(path, LossyPlugin, "-quality 80 "); + Compress(LossyPlugin, arguments); - private void Compress(string path, string compressionType, string switches = "") + File.Delete(path); + File.Move(tempPath, path); + } + + private void Compress(string compressionType, string arguments) { var processStartInfo = new ProcessStartInfo { UseShellExecute = false, CreateNoWindow = true, FileName = compressionType, - Arguments = $"{switches}-outfile {path} {path}" - - // FileName = "mozjpeg", - // Arguments = $"{compressionType} -quality 80 -outfile {path} {path}" + Arguments = arguments, }; using (var process = Process.Start(processStartInfo)) {