简介
本文档提供关于基础图片处理的 API 概览以及 SDK 示例代码。
基础图片处理
功能说明
支持灵活的图像编辑,例如图片旋转、裁剪、转码、缩放等。
下载时处理
方法原型
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"
resp, err := c.CI.Get(context.Background(), key, operation, nil)
if err != nil {
}
defer resp.Body.Close()
ioutil.ReadAll(resp.Body)
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 |
图片处理参数 |