ImageOptimApi 1.2.0
dotnet add package ImageOptimApi --version 1.2.0
NuGet\Install-Package ImageOptimApi -Version 1.2.0
<PackageReference Include="ImageOptimApi" Version="1.2.0" />
<PackageVersion Include="ImageOptimApi" Version="1.2.0" />
<PackageReference Include="ImageOptimApi" />
paket add ImageOptimApi --version 1.2.0
#r "nuget: ImageOptimApi, 1.2.0"
#:package ImageOptimApi@1.2.0
#addin nuget:?package=ImageOptimApi&version=1.2.0
#tool nuget:?package=ImageOptimApi&version=1.2.0
ImageOptimApi
This library provides access to the ImageOptim.com API from Microsoft .NET.
This is a third party library and just consumes the ImageOptim API. It is not directly affiliated with ImageOptim.com.
Issues found in this library should be filed at: https://github.com/MCLD/dotnet-imageoptim-api
ImageOptim.com is a Web service for image compression and optimization. You will need to register for a free trial username in order to use this library.
See sample C# code using this NuGet package at: MCLD/SampleImageOptimApi
Usage
Summary
- Install package with NuGet (
dotnet add ImageOptimApiorinstall-package ImageOptimApi). - Prepare an environment where you can inject an
ILoggerand anHttpClient. - Create an
ImageOptimApi.Client() - Configure the client object via properties, ensure you configure
Username. - Execute
OptimizeAsync().
ASP.NET Web application
Add the following packages to your project:
ImageOptimApi
After the builder object is created, add the ImageOptimApi.Client to the IServiceCollection. You can set some global parameters here for the HttpClient that will make the Web calls:
- Please leave
AllowAutoRedirecton per ImageOptim API documentation. - Configure the timeout here both for the
HttpClientas well as what is passed into the ImageOptim API. A good default is 30 seconds. - Please add a
UserAgentheader with the name of your product and the version.
builder.Services.AddHttpClient<ImageOptimApi.Client>()
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler
{
AllowAutoRedirect = true
})
.ConfigureHttpClient(_ =>
{
_.Timeout = TimeSpan.FromSeconds(30);
_.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("TestApp", "1.0.0"));
});
In your MVC controller or Razor PageModel, add an ImageOptimApi.Client parameter to the constructor and store it in a local variable. Here's a Razor Pages example:
private readonly ILogger<IndexModel> _logger;
private readonly Client _client;
public IndexModel(ILogger<IndexModel> logger, Client client)
{
_client = client ?? throw new ArgumentNullException(nameof(client));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
You can then call _client.OptimizeAsync() in your methods as needed.
Console application
Add the following packages to your project:
ImageOptimApiMicrosoft.Extensions.DependencyInjectionMicrosoft.Extensions.HttpMicrosoft.Extensions.Logging.Console
In your application, configure a dependency injection ServiceCollection and ServiceProvider. You can set some global parameters here for the HttpClient that will make the Web calls:
- Please leave
AllowAutoRedirecton per ImageOptim API documentation - Configure the timeout here both for the
HttpClientas well as what is passed into the ImageOptim API. A good default is 30 seconds. - Please add a
UserAgentheader with the name of your product and the version.
var services = new ServiceCollection();
services.AddLogging(_ => _.AddConsole());
services.AddHttpClient<Client>()
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler
{
AllowAutoRedirect = true
})
.ConfigureHttpClient(_ =>
{
_.Timeout = TimeSpan.FromSeconds(30);
_.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("TestApp", "1.0.0"));
});
var provider = services.BuildServiceProvider();
When you need the ImageOptimApi client you can obtain it from the ServiceProvider:
var client = provider.GetRequiredService<Client>();
client.Username = "<username>";
You can then configure and use the client to optimize your images.
Version History
- v1.2.0 - .NET 8.0 - adds the ability to work on an image provided as a byte array
- v1.1.0 - .NET 7.0
License
The ImageOptimApi source code is Copyright 2022 by the Maricopa County Library District and is distributed under The MIT License.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.AspNetCore.StaticFiles (>= 2.3.13)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.