From e8d7f1843d56fb93c9db22772e6c9bbd733fd1ea Mon Sep 17 00:00:00 2001 From: dabutvin Date: Sun, 10 May 2020 13:17:09 -0700 Subject: [PATCH] add gifsicle compression --- CompressImagesFunction/CompressImages.cs | 1 + .../Compressors/GifsicleCompress.cs | 50 +++++++++++++++++++ Dockerfile.CompressImages | 6 +++ 3 files changed, 57 insertions(+) create mode 100644 CompressImagesFunction/Compressors/GifsicleCompress.cs diff --git a/CompressImagesFunction/CompressImages.cs b/CompressImagesFunction/CompressImages.cs index b17eda88b..20e09240e 100644 --- a/CompressImagesFunction/CompressImages.cs +++ b/CompressImagesFunction/CompressImages.cs @@ -23,6 +23,7 @@ public static class CompressImages new ImageMagickCompress(), new SvgoCompress(), new MozJpegCompress(), + new GifsicleCompress(), }; public static bool Run(CompressimagesParameters parameters, ICollector compressImagesMessages, ILogger logger) diff --git a/CompressImagesFunction/Compressors/GifsicleCompress.cs b/CompressImagesFunction/Compressors/GifsicleCompress.cs new file mode 100644 index 000000000..e3af7559d --- /dev/null +++ b/CompressImagesFunction/Compressors/GifsicleCompress.cs @@ -0,0 +1,50 @@ +using System.Diagnostics; + +namespace CompressImagesFunction.Compressors +{ + /* + -O[level] + --optimize[=level] + Optimize output GIF animations for space. Level determines how much optimization is + done; higher levels take longer, but may have better results. There are currently + three levels: + + -O1 Stores only the changed portion of each image. This is the default. + -O2 Also uses transparency to shrink the file further. + -O3 Try several optimization methods (usually slower, sometimes better results). + + To modify GIF files in place, you should use the --batch option. With --batch, gifsicle + will modify the files you specify instead of writing a new file to the standard output. + */ + public class GifsicleCompress : ICompress + { + public string[] SupportedExtensions => new[] { ".gif" }; + + public void LosslessCompress(string path) + { + var arguments = $"-O3 --batch {path}"; + Compress(arguments); + } + + public void LossyCompress(string path) + { + var arguments = $"-O3 --lossy=80 --batch {path}"; + Compress(arguments); + } + + private void Compress(string arguments) + { + var processStartInfo = new ProcessStartInfo + { + UseShellExecute = false, + CreateNoWindow = true, + FileName = "gifsicle", + Arguments = arguments, + }; + using (var process = Process.Start(processStartInfo)) + { + process.WaitForExit(10000); + } + } + } +} diff --git a/Dockerfile.CompressImages b/Dockerfile.CompressImages index c8ae6d15e..d92b90ee2 100644 --- a/Dockerfile.CompressImages +++ b/Dockerfile.CompressImages @@ -27,5 +27,11 @@ 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 +# Add support for gifsicle +RUN cd /tmp && wget https://github.com/kohler/gifsicle/archive/v1.92.tar.gz +RUN cd /tmp && tar -xzf v1.92.tar.gz +RUN cd /tmp/gifsicle-1.92 && autoreconf -fiv +RUN cd /tmp/gifsicle-1.92 && sh ./configure --disable-gifview && make install + ENV AzureWebJobsScriptRoot=/home/site/wwwroot COPY --from=dotnet ["/home/site/wwwroot", "/home/site/wwwroot"]