Skip to content

Commit bd05ec6

Browse files
authored
Merge pull request #654 from dabutvin/gifsicle
add gifsicle compression
2 parents b105e73 + e8d7f18 commit bd05ec6

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

CompressImagesFunction/CompressImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public static class CompressImages
2323
new ImageMagickCompress(),
2424
new SvgoCompress(),
2525
new MozJpegCompress(),
26+
new GifsicleCompress(),
2627
};
2728

2829
public static bool Run(CompressimagesParameters parameters, ICollector<CompressImagesMessage> compressImagesMessages, ILogger logger)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.Diagnostics;
2+
3+
namespace CompressImagesFunction.Compressors
4+
{
5+
/*
6+
-O[level]
7+
--optimize[=level]
8+
Optimize output GIF animations for space. Level determines how much optimization is
9+
done; higher levels take longer, but may have better results. There are currently
10+
three levels:
11+
12+
-O1 Stores only the changed portion of each image. This is the default.
13+
-O2 Also uses transparency to shrink the file further.
14+
-O3 Try several optimization methods (usually slower, sometimes better results).
15+
16+
To modify GIF files in place, you should use the --batch option. With --batch, gifsicle
17+
will modify the files you specify instead of writing a new file to the standard output.
18+
*/
19+
public class GifsicleCompress : ICompress
20+
{
21+
public string[] SupportedExtensions => new[] { ".gif" };
22+
23+
public void LosslessCompress(string path)
24+
{
25+
var arguments = $"-O3 --batch {path}";
26+
Compress(arguments);
27+
}
28+
29+
public void LossyCompress(string path)
30+
{
31+
var arguments = $"-O3 --lossy=80 --batch {path}";
32+
Compress(arguments);
33+
}
34+
35+
private void Compress(string arguments)
36+
{
37+
var processStartInfo = new ProcessStartInfo
38+
{
39+
UseShellExecute = false,
40+
CreateNoWindow = true,
41+
FileName = "gifsicle",
42+
Arguments = arguments,
43+
};
44+
using (var process = Process.Start(processStartInfo))
45+
{
46+
process.WaitForExit(10000);
47+
}
48+
}
49+
}
50+
}

Dockerfile.CompressImages

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,11 @@ RUN cd /tmp/mozjpeg-3.3.1/build && sh ../configure && make install
2727
RUN ln -s /opt/mozjpeg/bin/jpegtran /usr/local/bin/mozjpegtran
2828
RUN ln -s /opt/mozjpeg/bin/cjpeg /usr/local/bin/mozcjpeg
2929

30+
# Add support for gifsicle
31+
RUN cd /tmp && wget https://github.com/kohler/gifsicle/archive/v1.92.tar.gz
32+
RUN cd /tmp && tar -xzf v1.92.tar.gz
33+
RUN cd /tmp/gifsicle-1.92 && autoreconf -fiv
34+
RUN cd /tmp/gifsicle-1.92 && sh ./configure --disable-gifview && make install
35+
3036
ENV AzureWebJobsScriptRoot=/home/site/wwwroot
3137
COPY --from=dotnet ["/home/site/wwwroot", "/home/site/wwwroot"]

0 commit comments

Comments
 (0)