有奖捉虫:云通信与企业服务文档专题,速来> HOT

简介

本文档提供关于图片高级压缩的 API 概览以及 SDK 示例代码。
API
操作描述
图片高级压缩可以更加高效地将图片转换为 WebP、 AVIF 、TPG 或 HEIF 等高压缩比格式,有效降低图片传输链路及加载耗时,降低带宽及流量成本。

图片高级压缩

功能说明

高效地将图片转换为 WebP、 AVIF 、TPG 或 HEIF 等高压缩比格式。

方法原型

func (s *CIService) GetToFile(ctx context.Context, key, localpath, operation string, opt *ObjectGetOptions, id ...string) (*Response, error)

请求示例

// 将原图转换为 WebP 格式
name := "test.png"
filepath := "test.webp"
_, err := c.CI.GetToFile(context.Background(), key, filepath, "imageMogr2/format/webp", nil)
if err != nil {
// ERROR
}

// 将原图转换为 AVIF 格式
filepath = "test.avif"
_, err = c.CI.GetToFile(context.Background(), key, filepath, "imageMogr2/format/avif", nil)
if err != nil {
// ERROR
}

// 将原图转换为 TPG 格式
name := "test.png"
filepath := "test.tpg"
_, err := c.CI.GetToFile(context.Background(), key, filepath, "imageMogr2/format/tpg", nil)
if err != nil {
// ERROR
}

// 将原图转换为 HEIF 格式
filepath = "test.heif"
_, err = c.CI.GetToFile(context.Background(), key, filepath, "imageMogr2/format/heif", nil)
if err != nil {
// ERROR
}

参数说明

参数名称
参数描述
key
对象键(Key)是对象在存储桶中的唯一标识。例如,在对象的访问域名examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/pic/pic.jpg中,对象键为 pic/pic.jpg
localpath
图片保存路径
operation
基础图片处理,可以通过该参数实现缩放、裁剪、旋转、格式转换、质量变换等基础图片处理功能。
opt
对象下载参数,详见 下载对象
id
对象 VersionId