.NET Standard Wrapper to allow for programmatic call for both OptiPNG and JPEGOptim to compress image. There is an addition nQuant PNG Quantizer which does not require the binary to be installed.
All Compression methods are lossy.
Allows piping for resizing, VirtualImage object etc.
All dependency needs to be avaliable via ${PATH}
PNGQuant - PNG Compression Tool. Minimum version 2.12.0
JPEGOptim - JPEG Compression Tool. Minimum version 1.4.4
Ensure Binary version are up to date!
Check PNGQuant version :
$ pngquant --verbose
Check JPEGOptim version :
$ jpegoptim --help
Install via .NET CLI
$ dotnet add package Kirinnee.Minimage
or
Install via NuGet Package Manager
PM> Install-Package Kirinnee.Minimage
Create a transformer which dictates what transformation (resize, compress etc) to apply to the images.
Without settings
ImageTransform jpegoptim = new JpegOptimTrasnformer();
With Settings
var options = new JpegOptimOptions(){
Quality = 80, //Maximum quality, default 76
StripAll = true, //Strip all metadata and markers, default true
Overwrite = false, //Overwrite target file if it already exist, default false
Progressive = false, //Make the image progressive. Will make the image not progessive if false. default true,
Force = true //Compress regardless of
};
///Invoke the compressor
ImageTransform jpegoptim = new JpegOptimTrasnformer(options);
Without Settings
ImageTransform pngquant = new PngQuantTransformer();
With Settings
//Options
var options = new PngQuantOptions(){
QualityMinMax = (65,80), //Minimum = 65, Maximum = 80. Default null
Spped = 1, //Value between 1 and 11. default 3.
IEBug = false, //Attempt to fix iebug. default false.
Bit = 256 //bit-rate. default 256
};
ImageTransform pngquant = new PngQuantTransformer(options);
High-speed quantizer, using Xialoin Wu's fast optimal color quantizer with nQuant. Image quality drops quite significantly, but runs really fast.
ImageTransform wuquant = new WuQuantTransformer();
Resize the image if the bytes matches the MimeType.
Unsupported MimeTypes will result in error thrown.
Supported MimeTypes:
image/png
image/jpeg
image/bmp
image/gif
image/tiff
image/x-icon
//Resize to 50% of width and height if mimetype is image/png or image/jpeg
ImageTransform resize = new PercentageResizeTransformer(0.5, new[]{"image/png","image/jpeg"});
Here, I will use wuquant
and resize
object as examples. Refer to above as to how they are created.
//Image
byte[] originalImage = File.ReadAllBytes("image.png");
//Trandition object invoke
var quantized = wuquant.Transform(originalImage);
var resized = resize.Transform(originalImage);
//Linq Chaining
var output = originalImage.Transform(wuqaunt).Transform(resize);
//Arguments: Absolute Path to root of image folder (for context), absolute path to read, absolute to write
VirtualImage image = new VirtualImage("from","from/image.png","to/image.png");
//Reads the files into the object
image.Read();
var output = image.Transform(wuquant).Transform(resize);
//Write to output destination
output.Write();
//Or use it content for something
byte[] images = output.Content;
byte[] originalImage = File.ReadAllBytes("image.png");
var output = originalImage | wuquant | resize | jepgoptim;
//Arguments: Absolute Path to root of image folder (for context), absolute path to read, absolute to write
VirtualImage image = new VirtualImage("from","from/image.png","to/image.png");
//Reads the files into the object
image.Read();
//Get Transformed virtual image
var output = image | wuquant | resize | jpegoptim;
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
We use SemVer for versioning. For the versions available, see the tags on this repository.
This project is licensed under MIT - see the LICENSE.md file for details