이것은 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
메서드가 제거되었습니다. 이제 Uri
개체를 사용하는 Compress
의 새로운 오버로드를 통해 사용할 수 있습니다.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 ) ;
TinyPng는 Wildcard
형식을 사용하여 제공된 이미지에 가장 적합한 유형을 반환합니다.
이미지로 변환하고 투명도를 잃는 시나리오에서는 이미지에 사용할 배경색을 지정할 수 있습니다.
var compressAndConvert = await png . Compress ( " cat.png " ) . Convert ( ConvertImageFormat . Wildcard , " #FF0000 " ) ;
압축 작업의 결과는 Amazon S3 스토리지에 직접 저장할 수 있습니다. 적절한 S3 액세스를 구성하는 방법에 대해서는 TinyPNG.com의 문서를 참조하는 것이 좋습니다.
대부분의 요청에 대한 이미지를 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 ) ;