這是圍繞 TinyPNG.com 圖像壓縮服務的 .NET Standard 包裝器。這不是 TinyPNG.com 的官方產品。
Byte[]
、 Stream
、 File
和Url
API 可用透過 Nuget 安裝
Install-Package TinyPNG
透過dotnet
安裝
dotnet add package TinyPNG
using var png = new TinyPngClient ( " yourSecretApiKey " ) ;
var result = await png . Compress ( " cat.jpg " ) ;
//URL to your compressed version
result . Output . Url ;
TinyPng
命名空間。這將避免需要引入兩個不同的名稱空間。CompressFromUrl
方法已被刪除。現在可以透過Compress
的新重載獲得此功能,該重載接受Uri
對象TinyPng
上的命名空間,之前它有點像是混合的外殼。 API 從 V2 開始發生了變化,主要是您不再需要等待使用 TinyPNG API 的每個單獨步驟,您現在可以將適當的呼叫連結在一起,因為擴充方法現在在Task<T>
上運行。
// create an instance of the TinyPngClient
using var png = new TinyPngClient ( " yourSecretApiKey " ) ;
// Create a task to compress an image.
// this gives you the information about your image as stored by TinyPNG
// they don't give you the actual bits (as you may want to chain this with a resize
// operation without caring for the originally sized image).
var compressImageTask = png . Compress ( " pathToFile or byte array or stream " ) ;
// or `CompressFromUrl` if compressing from a remotely hosted image.
var compressFromUrlImageTask = png . CompressFromUrl ( " image url " ) ;
// If you want to actually save this compressed image off
// it will need to be downloaded
var compressedImage = await compressImageTask . Download ( ) ;
// you can then get the bytes
var bytes = await compressedImage . GetImageByteData ( ) ;
// get a stream instead
var stream = await compressedImage . GetImageStreamData ( ) ;
// or just save to disk
await compressedImage . SaveImageToDisk ( " pathToSaveImage " ) ;
// Putting it all together
await png . Compress ( " path " )
. Download ( )
. SaveImageToDisk ( " savedPath " ) ;
有關壓縮結果的更多詳細資訊也可在Compress
操作的Input
和Output
屬性中找到。一些例子:
var result = await png . Compress ( " pathToFile or byte array or stream " ) ;
// old size
result . Input . Size ;
// new size
result . Output . Size ;
// URL of the compressed Image
result . Output . Url ;
using var png = new TinyPngClient ( " yourSecretApiKey " ) ;
var compressImageTask = png . Compress ( " pathToFile or byte array or stream " ) ;
var resizedImageTask = compressImageTask . Resize ( width , height ) ;
await resizedImageTask . SaveImageToDisk ( " pathToSaveImage " ) ;
// altogether now....
await png . Compress ( " pathToFile " )
. Resize ( width , height )
. SaveImageToDisk ( " pathToSaveImage " ) ;
指定調整大小選項時,某些組合與 TinyPNG 不相容。我們還包括強類型調整大小操作,具體取決於您想要執行的調整大小類型。
using var png = new TinyPngClient ( " yourSecretApiKey " ) ;
var compressTask = png . Compress ( " pathToFile or byte array or stream " ) ;
await compressTask . Resize ( new ScaleWidthResizeOperation ( width ) ) ;
await compressTask . Resize ( new ScaleHeightResizeOperation ( height ) ) ;
await compressTask . Resize ( new FitResizeOperation ( width , height ) ) ;
await compressTask . Resize ( new CoverResizeOperation ( width , height ) ) ;
Resize()
方法的結果中提供了相同的Byte[]
、 Stream
、 File
和Url
路徑 API。
您可以使用Convert()
方法將影像轉換為不同的格式。這將傳回一個包含轉換後的影像資料的物件。
using var png = new TinyPngClient ( " yourSecretApiKey " ) ;
var compressAndConvert = await png . Compress ( " cat.png " ) . Convert ( ConvertImageFormat . Wildcard ) ;
透過使用Wildcard
格式,TinyPng 將傳回所提供影像的最佳類型。
在轉換為影像並失去透明度的情況下,您可以指定用於影像的背景顏色。
var compressAndConvert = await png . Compress ( " cat.png " ) . Convert ( ConvertImageFormat . Wildcard , " #FF0000 " ) ;
任何壓縮操作的結果都可以直接儲存到 Amazon S3 儲存中。我強烈建議您參考 TinyPNG.com 的文檔,以了解如何設定適當的 S3 存取。
如果您要將大多數請求的映像儲存到 S3 中,那麼您可以將AmazonS3Configuration
物件傳遞給建構函數,該物件將用於所有後續請求。
using var png = new TinyPngClient ( " yourSecretApiKey " ,
new AmazonS3Configuration ( " awsAccessKeyId " , " awsSecretAccessKey " , " bucket " , " region " ) ) ;
var compressedCat = await png . Compress ( " cat.jpg " ) ;
var s3Uri = await png . SaveCompressedImageToAmazonS3 ( compressedCat , " file-name.png " ) ;
// If you'd like to override the particular bucket or region
// an image is being stored to from what is specified in the AmazonS3Configuration:
var s3UriInNewSpot = await png . SaveCompressedImageToAmazonS3 (
compressedCat ,
" file-name.png " ,
bucketOverride : " different-bucket " ,
regionOverride : " different-region " ) ;
您也可以將AmazonS3Configuration
物件直接傳遞到對SaveCompressedImageToAmazonS3
的呼叫中
using var png = new TinyPngClient ( " yourSecretApiKey " ) ;
var compressedCat = await png . Compress ( " cat.jpg " ) ;
var s3Uri = await png . SaveCompressedImageToAmazonS3 ( compressedCat ,
new AmazonS3Configuration (
" awsAccessKeyId " ,
" awsSecretAccessKey " ,
" bucket " ,
" region " ) , " file-name.png " ) ;
您可以透過檢查已執行的任何操作結果的CompressionCount
屬性來了解已執行的壓縮操作的數量。這對於密切關注 API 使用情況非常有用。
var compressedCat = await png . Compress ( " cat.jpg " ) ;
compressedCat . CompressionCount ; // = 5
TinyPngClient 可以將 HttpClient 作為建構函式重載,其生命週期可以從函式庫外部控制。
var httpClient = new HttpClient ( ) ;
var png = new TinyPngClient ( " yourSecretApiKey " , httpClient ) ;