-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #495 from ElSiipo/master
add support for mozJpeg compression
- Loading branch information
Showing
3 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters