Skip to content

Commit

Permalink
updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ctolkien committed Jan 28, 2016
1 parent 56c1876 commit f7c7831
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 13 deletions.
67 changes: 63 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
20 changes: 17 additions & 3 deletions src/TinyPNG/TinyPng.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,16 @@ public async Task<TinyPngCompressResponse> Compress(Stream data)
}


public async Task<TinyPngResizeResponse> Resize(TinyPngCompressResponse result, ResizeOperation resizeOp)
/// <summary>
/// Uses the TinyPng API to create a resized version of your uploaded image.
/// </summary>
/// <param name="result">This is the previous result of running a compression <see cref="Compress(string)"/></param>
/// <param name="resizeOperation">Supply a strongly typed Resize Operation. See <typeparamref name="CoverResizeOperation"/>, <typeparamref name="FitResizeOperation"/>, <typeparamref name="ScaleHeightResizeOperation"/>, <typeparamref name="ScaleWidthResizeOperation"/></param>
/// <returns></returns>
public async Task<TinyPngResizeResponse> 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);
Expand All @@ -125,7 +131,15 @@ public async Task<TinyPngResizeResponse> Resize(TinyPngCompressResponse result,
throw new TinyPngApiException((int)response.StatusCode, response.ReasonPhrase, errorMsg.Error, errorMsg.Message);
}

public async Task<TinyPngResizeResponse> Resize(TinyPngCompressResponse result, int height, int width, ResizeType resizeType = ResizeType.Fit)
/// <summary>
/// Uses the TinyPng API to create a resized version of your uploaded image.
/// </summary>
/// <param name="result">This is the previous result of running a compression <see cref="Compress(string)"/></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="resizeType"></param>
/// <returns></returns>
public async Task<TinyPngResizeResponse> Resize(TinyPngCompressResponse result, int width, int height, ResizeType resizeType = ResizeType.Fit)
{
var resizeOp = new ResizeOperation(resizeType, width, height);

Expand Down
7 changes: 1 addition & 6 deletions tests/TinyPng.Tests/TinyPngTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -35,7 +31,6 @@ public async Task Resizing()
var resized = await pngx.Resize(result, new ScaleHeightResizeOperation(100));

Assert.Equal(7085, (await resized.GetImageByteData()).Length);


}
}
Expand Down

0 comments on commit f7c7831

Please sign in to comment.