Skip to content

Commit

Permalink
Merge pull request #495 from ElSiipo/master
Browse files Browse the repository at this point in the history
add support for mozJpeg compression
  • Loading branch information
dabutvin committed Oct 15, 2019
2 parents a6da825 + 51955bd commit 160c5cb
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CompressImagesFunction/CompressImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static class CompressImages
{
new ImageMagickCompress(),
new SvgoCompress(),
new MozJpegCompress(),
};

public static bool Run(CompressimagesParameters parameters, ILogger logger)
Expand Down Expand Up @@ -177,6 +178,7 @@ private static CompressionResult[] OptimizeImages(Repository repo, string localP
if (aggressiveCompression)
{
optimizer.LossyCompress(image);
optimizer.LosslessCompress(image);
}
else
{
Expand Down
46 changes: 46 additions & 0 deletions CompressImagesFunction/Compressors/MozJpegCompress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Diagnostics;
using System.IO;

namespace CompressImagesFunction.Compressors
{
public class MozJpegCompress : ICompress
{
private static readonly string LosslessPlugin = "mozjpegtran";
private static readonly string LossyPlugin = "mozcjpeg";

public string[] SupportedExtensions =>
new[] { ".jpg", ".jpeg" };

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}";

Compress(LossyPlugin, arguments);

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 = arguments,
};
using (var process = Process.Start(processStartInfo))
{
process.WaitForExit(10000);
}
}
}
}
12 changes: 11 additions & 1 deletion Dockerfile.CompressImages
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +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

# 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"]

0 comments on commit 160c5cb

Please sign in to comment.