From f7c78314c23518477c51a004b9f67d5d1887ed10 Mon Sep 17 00:00:00 2001 From: Chad Tolkien Date: Thu, 28 Jan 2016 13:38:59 +1100 Subject: [PATCH] updated readme --- README.md | 67 +++++++++++++++++++++++++++-- src/TinyPNG/TinyPng.cs | 20 +++++++-- tests/TinyPng.Tests/TinyPngTests.cs | 7 +-- 3 files changed, 81 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 4690e88..46b974a 100644 --- a/README.md +++ b/README.md @@ -4,15 +4,74 @@ This is a .NET wrapper around the TinyPng.com image compression service. * Supports .Net Core and full .Net Framework * Non-blocking async turtles all the way down -* Byte, stream and file path API's available +* `Byte[]`, `Stream` and `File` API's available -## Usage +## Compressing Image ```csharp -using (var png = new TinyPng("apiKey")) +using (var png = new TinyPng("yourSecretApiKey")) { - await (await png.Compress("pathToFile")).SaveImageToDisk("PathToSave"); + var compressResult = await png.Compress("pathToFile or byte array or stream)"); + + //get the image data as a byte array + var bytes = await compressResult.GetImageByteData(); + + //get a stream instead + var stream = await compressResult.GetImageStreamData() + + //or just save to disk + await compressResult.SaveImageToDisk("pathToSaveImage"); + } ``` + +Further details about the result of the compression are also available on the `Input` and `Output` properties. +```csharp + + //old size + compressResult.Input.Size; + + //new size + compressResult.Output.Size; + + //URL of the compressed Image + compressResult.Output.Url; + +``` + +## Resizing Images + +```csharp +using (var png = new TinyPng("yourSecretApiKey")) +{ + var compressResult = await png.Compress("pathToFile or byte array or stream)"); + + var resizedImage = await png.Resize(compressResult, width, height, ResizeType); + + await resizedImage.SaveImageToDisk("pathToSaveImage"); +} + +``` + +### Resize Operations + +There are certain combinations when specifying resize options which aren't compatible with +TinyPNG. We also include strongly type resize operations, +depending on the type of resize you want to do. + +```csharp +using (var png = new TinyPng("yourSecretApiKey")) +{ + var compressResult = await png.Compress("pathToFile or byte array or stream)"); + + await png.Resize(compressResult, new ScaleWidthResizeOperation(width)); + await png.Resize(compressResult, new ScaleHeightResizeOperation(width)); + await png.Resize(compressResult, new FitResizeOperation(width, height)); + await png.Resize(compressResult, new CoverResizeOperation(width, height)); +} + +``` + +The same `Byte[]`, `Stream` and `File` path API's are available from the result of the `Resize()` method. \ No newline at end of file diff --git a/src/TinyPNG/TinyPng.cs b/src/TinyPNG/TinyPng.cs index 23f8e72..ca3eede 100644 --- a/src/TinyPNG/TinyPng.cs +++ b/src/TinyPNG/TinyPng.cs @@ -106,10 +106,16 @@ public async Task Compress(Stream data) } - public async Task Resize(TinyPngCompressResponse result, ResizeOperation resizeOp) + /// + /// Uses the TinyPng API to create a resized version of your uploaded image. + /// + /// This is the previous result of running a compression + /// Supply a strongly typed Resize Operation. See , , , + /// + public async Task Resize(TinyPngCompressResponse result, ResizeOperation resizeOperation) { - var requestBody = JsonConvert.SerializeObject(new { resize = resizeOp }, jsonSettings); + var requestBody = JsonConvert.SerializeObject(new { resize = resizeOperation }, jsonSettings); var msg = new HttpRequestMessage(HttpMethod.Post, result.Output.Url); @@ -125,7 +131,15 @@ public async Task Resize(TinyPngCompressResponse result, throw new TinyPngApiException((int)response.StatusCode, response.ReasonPhrase, errorMsg.Error, errorMsg.Message); } - public async Task Resize(TinyPngCompressResponse result, int height, int width, ResizeType resizeType = ResizeType.Fit) + /// + /// Uses the TinyPng API to create a resized version of your uploaded image. + /// + /// This is the previous result of running a compression + /// + /// + /// + /// + public async Task Resize(TinyPngCompressResponse result, int width, int height, ResizeType resizeType = ResizeType.Fit) { var resizeOp = new ResizeOperation(resizeType, width, height); diff --git a/tests/TinyPng.Tests/TinyPngTests.cs b/tests/TinyPng.Tests/TinyPngTests.cs index 59a3be4..a5f7e02 100644 --- a/tests/TinyPng.Tests/TinyPngTests.cs +++ b/tests/TinyPng.Tests/TinyPngTests.cs @@ -12,17 +12,13 @@ public async Task Compression() { var pngx = new TinyPng(apiKey); - var result = await pngx.Compress("Resources/cat.jpg"); - Assert.Equal("image/jpeg", result.Input.Type); - Assert.Equal(300, result.Output.Width); + Assert.Equal(300, result.Output.Size); Assert.Equal(182, (await result.GetImageByteData()).Length); - - } [Fact(Skip = "integration")] @@ -35,7 +31,6 @@ public async Task Resizing() var resized = await pngx.Resize(result, new ScaleHeightResizeOperation(100)); Assert.Equal(7085, (await resized.GetImageByteData()).Length); - } }