Skip to content

Commit

Permalink
Merge pull request #654 from dabutvin/gifsicle
Browse files Browse the repository at this point in the history
add gifsicle compression
  • Loading branch information
dabutvin authored May 10, 2020
2 parents b105e73 + e8d7f18 commit bd05ec6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions CompressImagesFunction/CompressImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static class CompressImages
new ImageMagickCompress(),
new SvgoCompress(),
new MozJpegCompress(),
new GifsicleCompress(),
};

public static bool Run(CompressimagesParameters parameters, ICollector<CompressImagesMessage> compressImagesMessages, ILogger logger)
Expand Down
50 changes: 50 additions & 0 deletions CompressImagesFunction/Compressors/GifsicleCompress.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
6 changes: 6 additions & 0 deletions Dockerfile.CompressImages
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

0 comments on commit bd05ec6

Please sign in to comment.