Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add gifsicle compression #654

Merged
merged 1 commit into from
May 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]