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

简介

本文档提供关于基础图片处理的 API 概览以及 SDK 示例代码。
API
操作描述
对图片进行缩小或放大
对图片进行裁剪,包括普通裁剪、缩放裁剪、内切圆裁剪、圆角裁剪和人脸智能裁剪
对图片进行旋转,包括普通旋转和自适应旋转
对图片进行格式转换、gif 格式优化、渐进显示
对图片质量进行调节
对图片进行模糊处理
对图片进行亮度调节
对图片进行对比度调节
对图片进行锐化
将图片设置为灰度图
对图片进行水印处理
对图片进行实时文字水印处理
查询图片基本信息,包括格式、长、宽等
查询 EXIF 信息
查询图片主色调信息
去除图片元信息,包括 exif 信息
通过图片处理模板,生成相应的缩略图
限制图片处理(例如缩放、压缩等)后的文件大小
实现对图片按顺序进行多种处理

基础图片处理

功能说明

支持灵活的图像编辑,例如图片旋转、裁剪、转码、缩放等。

下载时处理

方法原型

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

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

请求示例 (以缩放为例)

key := "test.jpg"
operation := "imageMogr2/thumbnail/!50px"
// Case 1 从响应体中获取对象
resp, err := c.CI.Get(context.Background(), key, operation, nil)
if err != nil {
//ERROR
}
defer resp.Body.Close()
ioutil.ReadAll(resp.Body)

// Case 2 下载对象到文件
filepath := "test.jpg"
_, err = c.CI.GetToFile(context.Background(), key, filepath, operation, nil)

参数说明

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

上传时处理

方法原型

func (s *CIService) PutFromFile(ctx context.Context, key string, filePath string, opt *ObjectPutOptions) (*ImageProcessResult, *Response, error)

请求示例 (以缩放为例)

obj := "pic/upload/deer.jpg"
localpath := "./deer.jpg"
operation := "imageMogr2/thumbnail/!50px"
pic := &cos.PicOperations{
IsPicInfo: 1,
Rules: []cos.PicOperationsRules{
{
FileId: "thumbnail_50.jpg",
Rule: operation,
},
},
}
opt := &cos.ObjectPutOptions{
nil,
&cos.ObjectPutHeaderOptions{
XOptionHeader: &http.Header{},
},
}
opt.XOptionHeader.Add("Pic-Operations", cos.EncodePicOperations(pic))
res, _, err := c.CI.PutFromFile(context.Background(), obj, localpath, opt)

参数说明

参数名称
参数描述
key
对象键(key)是对象在存储桶中的唯一标识。例如,在对象的访问域名examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/pic/pic.jpg中,对象键为 pic/pic.jpg
filePath
本地文件的路径
opt
对象上传参数,详见 上传对象

云上数据处理

方法原型

func (s *CIService) ImageProcess(ctx context.Context, key string, opt *ImageProcessOptions) (*ImageProcessResult, *Response, error)

请求示例 (以缩放为例)

obj := "pic/upload/deer.jpg"
operation := "imageMogr2/thumbnail/!50px"
pic := &cos.PicOperations{
IsPicInfo: 1,
Rules: []cos.PicOperationsRules{
{
FileId: "thumbnail_50.jpg",
Rule: operation,
},
},
}
opt.XOptionHeader.Add("Pic-Operations", cos.EncodePicOperations(pic))
res, _, err := c.CI.ImageProcess(context.Background(), obj, pic)

参数说明

参数名称
参数描述
key
对象键(key)是对象在存储桶中的唯一标识。例如,在对象的访问域名examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/pic/pic.jpg中,对象键为 pic/pic.jpg
opt
图片处理参数