Skip to content

Commit 94de178

Browse files
authored
Merge pull request #323 from tencentyun/feature_jojoliang_c66c88cb
Feature jojoliang c66c88cb
2 parents 3caf239 + fa0874d commit 94de178

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed

ci.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,25 @@ func (s *CIService) ImageProcess(ctx context.Context, name string, opt *ImagePro
110110
return &res, resp, err
111111
}
112112

113+
type ImageProcessHeader struct {
114+
PicOperations string `header:"Pic-Operations" xml:"-" url:"-"`
115+
XOptionHeader *http.Header `header:"-,omitempty" url:"-" xml:"-"`
116+
}
117+
118+
// 云上数据处理 https://cloud.tencent.com/document/product/460/18147
119+
func (s *CIService) ImageProcessWithHeader(ctx context.Context, name string, opt *ImageProcessHeader) (*ImageProcessResult, *Response, error) {
120+
var res ImageProcessResult
121+
sendOpt := sendOptions{
122+
baseURL: s.client.BaseURL.BucketURL,
123+
uri: "/" + encodeURIComponent(name) + "?image_process",
124+
method: http.MethodPost,
125+
optHeader: opt,
126+
result: &res,
127+
}
128+
resp, err := s.client.send(ctx, &sendOpt)
129+
return &res, resp, err
130+
}
131+
113132
// ImageRecognitionOptions is the option of ImageAuditing
114133
type ImageRecognitionOptions struct {
115134
CIProcess string `url:"ci-process,omitempty"`

ci_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,106 @@ func TestCIService_ImageProcess(t *testing.T) {
139139
}
140140
}
141141

142+
func TestCIService_ImageProcessWithHeader(t *testing.T) {
143+
setup()
144+
defer teardown()
145+
name := "test.jpg"
146+
147+
opt := &ImageProcessOptions{
148+
IsPicInfo: 1,
149+
Rules: []PicOperationsRules{
150+
{
151+
FileId: "format.jpg",
152+
Rule: "imageView2/format/png",
153+
},
154+
},
155+
}
156+
157+
mux.HandleFunc("/test.jpg", func(w http.ResponseWriter, r *http.Request) {
158+
testMethod(t, r, "POST")
159+
vs := values{
160+
"image_process": "",
161+
}
162+
testFormValues(t, r, vs)
163+
header := r.Header.Get("Pic-Operations")
164+
body := new(ImageProcessOptions)
165+
err := json.Unmarshal([]byte(header), body)
166+
want := opt
167+
if err != nil {
168+
t.Errorf("CI.ImageProcess Failed: %v", err)
169+
}
170+
if !reflect.DeepEqual(want, body) {
171+
t.Errorf("CI.ImageProcess Failed, wanted:%v, body:%v", want, body)
172+
}
173+
fmt.Fprint(w, `<UploadResult>
174+
<OriginalInfo>
175+
<Key>test.jpg</Key>
176+
<Location>example-1250000000.cos.ap-guangzhou.myqcloud.com/test.jpg</Location>
177+
<ETag>&quot;8894dbe5e3ebfaf761e39b9d619c28f3327b8d85&quot;</ETag>
178+
<ImageInfo>
179+
<Format>PNG</Format>
180+
<Width>103</Width>
181+
<Height>99</Height>
182+
<Quality>100</Quality>
183+
<Ave>0xa08162</Ave>
184+
<Orientation>0</Orientation>
185+
</ImageInfo>
186+
</OriginalInfo>
187+
<ProcessResults>
188+
<Object>
189+
<Key>format.jpg</Key>
190+
<Location>example-1250000000.cos.ap-guangzhou.myqcloud.com/format.jpg</Location>
191+
<Format>PNG</Format>
192+
<Width>103</Width>
193+
<Height>99</Height>
194+
<Size>21351</Size>
195+
<Quality>100</Quality>
196+
<ETag>&quot;8894dbe5e3ebfaf761e39b9d619c28f3327b8d85&quot;</ETag>
197+
</Object>
198+
</ProcessResults>
199+
</UploadResult>`)
200+
})
201+
202+
want := &ImageProcessResult{
203+
XMLName: xml.Name{Local: "UploadResult"},
204+
OriginalInfo: &PicOriginalInfo{
205+
Key: "test.jpg",
206+
Location: "example-1250000000.cos.ap-guangzhou.myqcloud.com/test.jpg",
207+
ETag: "\"8894dbe5e3ebfaf761e39b9d619c28f3327b8d85\"",
208+
ImageInfo: &PicImageInfo{
209+
Format: "PNG",
210+
Width: 103,
211+
Height: 99,
212+
Quality: 100,
213+
Ave: "0xa08162",
214+
Orientation: 0,
215+
},
216+
},
217+
ProcessResults: []PicProcessObject{
218+
{
219+
Key: "format.jpg",
220+
Location: "example-1250000000.cos.ap-guangzhou.myqcloud.com/format.jpg",
221+
Format: "PNG",
222+
Width: 103,
223+
Height: 99,
224+
Size: 21351,
225+
Quality: 100,
226+
ETag: "\"8894dbe5e3ebfaf761e39b9d619c28f3327b8d85\"",
227+
},
228+
},
229+
}
230+
optHeader := &ImageProcessHeader{
231+
PicOperations: EncodePicOperations(opt),
232+
}
233+
res, _, err := client.CI.ImageProcessWithHeader(context.Background(), name, optHeader)
234+
if err != nil {
235+
t.Fatalf("CI.ImageProcess returned error: %v", err)
236+
}
237+
if !reflect.DeepEqual(res, want) {
238+
t.Errorf("CI.ImageProcess failed, return:%v, want:%v", res, want)
239+
}
240+
}
241+
142242
func TestCIService_ImageRecognition(t *testing.T) {
143243
setup()
144244
defer teardown()

example/CI/image_process/base_process.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,29 @@ func processWhenCloud(ctx context.Context, rawurl, obj string, pic *cos.PicOpera
137137
fmt.Printf("%+v\n", res.ProcessResults)
138138
}
139139

140+
// 云上数据处理
141+
func processWhenCloudWithHeader(ctx context.Context, rawurl, obj string, imageProcessHeader *cos.ImageProcessHeader ) {
142+
u, _ := url.Parse(rawurl)
143+
b := &cos.BaseURL{BucketURL: u}
144+
c := cos.NewClient(b, &http.Client{
145+
Transport: &cos.AuthorizationTransport{
146+
SecretID: os.Getenv("COS_SECRETID"),
147+
SecretKey: os.Getenv("COS_SECRETKEY"),
148+
Transport: &debug.DebugRequestTransport{
149+
RequestHeader: true,
150+
RequestBody: false,
151+
ResponseHeader: true,
152+
ResponseBody: true,
153+
},
154+
},
155+
})
156+
res, _, err := c.CI.ImageProcessWithHeader(ctx, obj, imageProcessHeader)
157+
log_status(err)
158+
fmt.Printf("%+v\n", res)
159+
fmt.Printf("%+v\n", res.OriginalInfo)
160+
fmt.Printf("%+v\n", res.ProcessResults)
161+
}
162+
140163
// 添加盲水印
141164
func blindWatermark() {
142165
rawurl := "https://test-1234567890.cos.ap-chongqing.myqcloud.com"
@@ -1097,6 +1120,7 @@ func textWatermarkAndAIGC() {
10971120
}
10981121
processWhenUpload(context.Background(), rawurl, obj, filepath, pic)
10991122
}
1123+
11001124
// 云上数据处理
11011125
{
11021126
obj := "pic/deer.jpeg"
@@ -1111,6 +1135,27 @@ func textWatermarkAndAIGC() {
11111135
}
11121136
processWhenCloud(context.Background(), rawurl, obj, pic)
11131137
}
1138+
1139+
// 云上数据处理
1140+
{
1141+
obj := "pic/deer.jpeg"
1142+
pic := &cos.PicOperations{
1143+
IsPicInfo: 1,
1144+
Rules: []cos.PicOperationsRules{
1145+
{
1146+
FileId: "textwatermark/textwatermark1.jpg",
1147+
Rule: "imageMogr2/" + AIGCMetadata + "|watermark/2/text/6IW-6K6v5LqRwrfkuIfosaHkvJjlm74/fill/IzNEM0QzRA/fontsize/20/dissolve/50/gravity/northeast/dx/20/dy/20/batch/1/degree/45",
1148+
},
1149+
},
1150+
}
1151+
imageProcessHeader := &cos.ImageProcessHeader{
1152+
PicOperations: cos.EncodePicOperations(pic),
1153+
XOptionHeader: &http.Header{},
1154+
}
1155+
imageProcessHeader.XOptionHeader.Add("x-cos-meta-aigctag", "csig")
1156+
processWhenCloudWithHeader(context.Background(), rawurl, obj, imageProcessHeader)
1157+
}
1158+
11141159
}
11151160

11161161
func main() {

0 commit comments

Comments
 (0)