这是围绕 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 ) ;