⚠ Deprecated: Legacy
Suggested alternative: TinyPNG
.NET client for the Tinify API. Tinify compresses your images intelligently. Read more at https://tinify.com.
$ dotnet add package Tinify.Unofficial
.NET client for the Tinify API, used for TinyPNG and TinyJPG. Tinify compresses your images intelligently. Read more at http://tinify.com.
Install the API client:
Install-Package Tinify.Unofficial
The Shrink methods on the TinifyClient were renamed:
ShrinkFromFile => ShrinkFromFileAsyncShrinkFromBuffer => ShrinkFromBufferAsyncShrinkFromUrl => ShrinkFromUrlAsyncSimply construct a new instance of TinifyClient with your API key
using Tinify.Unofficial;
var client = new TinifyClient("YOUR_API_KEY");
The client constructor also allows for the specifying of an HttpMessageHandler which allows you to manage any kind of custom or specialized handling needed: specifying a proxy, special SSL handling, etc. The does not assume ownership of this handler, you are responsible for managing its lifetime.
TinifyClientIf no handler is specified then the default SocketsHttpHandler
with a PooledConnectionLifetime setting of 5 minutes is used. (Note: this also applies to the .netstandard 2.1 target
see StandardSocketsHttpHandler)
public class MyProxy : IWebProxy
{
private readonly Uri _uri;
public ICredentials Credentials { get; set; }
public MyProxy(string url)
{
Uri.TryCreate(url, UriKind.Absolute, out _uri));
var user = _uri.UserInfo.Split(':');
Credentials = new NetworkCredential(user[0], user[1]);
}
public Uri GetProxy(Uri destination) => _uri;
public bool IsBypassed(Uri host) => host.IsLoopback;
}var client = new TinifyClient("YOUR_API_KEY",
new HttpClientHandler()
{
Proxy = new MyProxy(@"http://user:pass@localhost:8080"),
UserProxy = true,
});ShrinkFrom methodsShrinkFromFileAsyncShrinkFromBufferAsyncShrinkFromUrlAsyncShrinkFromStreamAsyncvar client = new TinifyClient("YOUR_API_KEY");
await using var optimizedImage = await client.ShrinkFromFileAsync("unoptimized.png");
await optimizedImage.ToFileAsync("optimized.png");This will upload the image to the Tinify image service and will return an OptimizedImage
You can check your current compression count
by accessing the static CompressionCount property of the TinifyClient class. This count is updated each
time a response from the Tinify endpoint returns a compression count.
var currentCompressionCount = TinifyClient.CompressionCount;To perform other transform operations (resize, convert, etc) simply call the TransformImage method
on the optimized image. The TransformImage operation takes a single TransformOperations argument.
The TransformOperations object must be constructed with at least one operation. Each of the Tinify
operations are by the different operations that can be specified in the TransformOperations constructor.
| Tinify API Operation | Tinify.Unofficial.Operation |
|---|---|
| resize | ResizeOperation |
| convert | ConvertOperation |
| preserve | PreserveOperation |
| store | CloudStoreOperation* |
*note: CloudStoreOperation is an abstract class and has concrete implementations
as AwsCloudStoreOperation and GoogleCloudStoreOperation
await using var optimizedImage = await client.ShrinkFromFile("unoptimized.jpg");
var resizeOptions = new ResizeOperation(ResizeType.Fit, 50, 20);
var preserveOptions = new PreserveOperation(PreserveOptions.Copyright | PreserveOptions.Creation);
var transformOperations = new TransformOperations(resize: resizeOptions, preserve: preserveOptions);
await using var result = await optimizedImage.TransformImage(transformOperations);
await result.ToFileAsync("optimized_and_transformed.jpg");Note:
Because both OptimizedImage and ImageResult objects maintain an internal buffer
of the image data, which has been rented from
the ArrayPool,
you should be sure to Dispose of them so that the buffer is returned to the pool.
Both objects implement IDisposable and IAsyncDisposable so that they
can be easily wrapped in using blocks or statements.
You can save both OptimizedImage and ImageResult objects to a file, to a stream, to a buffer or pass in a
preallocated buffer and copy the data directly to the buffer
await using var optimizedImage = await client.ShrinkFromFile("unoptimized.jpg");
await using var transformedImage =
await optimizedImage.TransformImage(new TransformOperations(
resize: new ResizeOperation(ResizeType.Fit, 50, 20)
));
var optimizedBuffer = await optimizedImage.ToBufferAsync();
// Note the ImageResult object already holds an internal buffer
// with the image data and so will just return a copy synchronously
var transformedBuffer = transformedImage.ToBuffer();
using var msOptimized = new MemoryStream();
await optimizedImage.ToStreamAsync(msOptimized);
using var msTransformed = new MemoryStream();
await transformedImage.ToStreamAsync(msTransformed);
var bufferOptimized = new byte[optimizedImage.ImageSize.Value];
await optimizedImage.CopyToBufferAsync(bufferOptimized);
// Note the ImageResult object already holds an internal buffer
// with the image data and so will just copy the data synchronously
var bufferTransformed = new byte[transformedImage.DataLength];
transformedImage.CopyToBuffer(bufferTransformed);dotnet restore
dotnet test test/Tinify.Unofficial.Tests
dotnet restore
TINIFY_KEY=$YOUR_API_KEY dotnet test test/Tinify.Unofficial.Tests.Integration
Or add a .env file to the /test/Tinify.Unofficial.Tests.Integration directory in the format
TINIFY_KEY=<YOUR_API_KEY>
This software is licensed under the MIT License. View the license.