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

简介

本文档提供关于图片持久化处理的 API 概览以及 SDK 示例代码。
API
说明
对象存储(Cloud Object Storage,COS)提供的上传时处理功能可以帮助使用者在上传时实现图片处理。此外能够对已存储在 COS 的图片进行相应处理操作,并将结果存入到 COS

上传时处理

示例代码

<?php

require dirname(__FILE__) . '/../vendor/autoload.php';

$secretId = "SECRETID"; //替换为用户的 secretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi
$secretKey = "SECRETKEY"; //替换为用户的 secretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi
$region = "ap-beijing"; //替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket
$cosClient = new Qcloud\\Cos\\Client(
array(
'region' => $region,
'scheme' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));

try {
$imageMogrTemplate = new Qcloud\\Cos\\ImageParamTemplate\\ImageMogrTemplate();//创建基础图片处理参数模板实例
$imageMogrTemplate->thumbnailByScale(50);//指定图片的宽高为原图的 50%
$picOperationsTemplate = new \\Qcloud\\Cos\\ImageParamTemplate\\PicOperationsTransformation();//创建图片持久化处理参数模版实例
$picOperationsTemplate->setIsPicInfo(1);//设置是否返回原图信息,0不返回原图信息,1返回原图信息,默认为0
$picOperationsTemplate->addRule($imageMogrTemplate, "resultobject");//设置图片处理规则
$result = $cosClient->putObject(array(
'Bucket' => 'examplebucket-1250000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
'Key' => 'exampleobject',
'Body' => fopen('path/to/localFile', 'rb'),
'PicOperations' => $picOperationsTemplate->queryString(),//生成图片持久化处理参数
));
// 请求成功
print_r($result);
} catch (\\Exception $e) {
// 请求失败
echo($e);
}

参数说明

参数名称
类型
描述
是否必填
Bucket
String
存储桶名称,格式:BucketName-APPID
Key
String
此处的 Key 为对象键,对象键是对象在存储桶中的唯一标识。例如,在对象的访问域名examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg 中,对象键为doc/pic.jpg
Body
File/String
上传的内容
PicOperations
Json/String
图片持久化处理信息

返回结果示例

Guzzle\\Service\\Resource\\Model Object
(
[structure:protected] =>
[data:protected] => Array
(
[Body] =>
[ETag] => "698d51a19d8a121ce581499d7b701668"
[RequestId] => NWQwOGRkNDdfMjJiMjU4NjRfNzVjXzEwNmVjY2M=
[ContentLength] => 238186
[Key] => exampleobject
[Bucket] => examplebucket-1250000000
[Location] => examplebucket-1250000000.cos.ap-beijing.myqcloud.com/exampleobject
[Data] => Array
(
[OriginalInfo] => Array
(
[Key] => exampleobject
[Location] => examplebucket-1250000000.cos.ap-beijing.myqcloud.com/exampleobject
[ETag] => "7037fb6fb4cca43b958a28789605e73d98088720"
[ImageInfo] => Array
(
[Format] => JPEG
[Width] => 600
[Height] => 500
[Quality] => 90
[Ave] => 0x46442e
[Orientation] => 0
)

)
[ProcessResults] => Array
(
[Object] => Array
(
[0] => Array(
[Key] => resultobject
[Location] => examplebucket-1250000000.cos.ap-beijing.myqcloud.com/resultobject
[Format] => JPEG
[Width] => 300
[Height] => 200
[Size] => 30000
[Quality] => 90
[ETag] => "87c153bc2909aa0ba111ca126b675c510d36b817"
)
)
)
)
)
)


返回结果说明

参数名称
类型
描述
父节点
Body
File/String
返回体
ETag
String
文件的 MD5 值
RequestId
String
请求 ID 标识
ContentLength
Int
响应体长度
Key
String
对象键
Bucket
String
存储桶名称,格式:BucketName-APPID
Location
String
请求资源地址
Data
Array
图片处理结果信息

云上数据处理

示例代码

<?php

require dirname(__FILE__) . '/../vendor/autoload.php';

$secretId = "SECRETID"; //替换为用户的 secretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi
$secretKey = "SECRETKEY"; //替换为用户的 secretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi
$region = "ap-beijing"; //替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket
$cosClient = new Qcloud\\Cos\\Client(
array(
'region' => $region,
'scheme' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));

try {
$imageMogrTemplate = new Qcloud\\Cos\\ImageParamTemplate\\ImageMogrTemplate();//创建基础图片处理参数模版实例
$imageMogrTemplate->thumbnailByScale(50);//指定图片的宽高为原图的 50%
$picOperationsTemplate = new \\Qcloud\\Cos\\ImageParamTemplate\\PicOperationsTransformation();//创建图片持久化处理参数模版实例
$picOperationsTemplate->setIsPicInfo(1);//设置是否返回原图信息,0不返回原图信息,1返回原图信息,默认为0
$picOperationsTemplate->addRule($imageMogrTemplate, "resultobject");//设置图片处理规则
$result = $cosClient->ImageProcess(array(
'Bucket' => 'examplebucket-1250000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
'Key' => 'exampleobject',
'PicOperations' => $picOperationsTemplate->queryString(),//生成图片持久化处理参数
));
// 请求成功
print_r($result);
} catch (\\Exception $e) {
// 请求失败
echo($e);
}

参数说明

参数名称
类型
描述
是否必填
Bucket
String
存储桶名称,格式:BucketName-APPID
Key
String
此处的 Key 为对象键,对象键是对象在存储桶中的唯一标识。例如,在对象的访问域名examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg 中,对象键为doc/pic.jpg
PicOperations
Json/String
图片持久化处理信息

返回结果示例

Guzzle\\Service\\Resource\\Model Object
(
[structure:protected] =>
[data:protected] => Array
(
[RequestId] => NWQwOGRkNDdfMjJiMjU4NjRfNzVjXzEwNmVjY2M=
[Key] => exampleobject
[Bucket] => examplebucket-1250000000
[Location] => examplebucket-1250000000.cos.ap-beijing.myqcloud.com/exampleobject
[OriginalInfo] => Array
(
[Key] => exampleobject
[Location] => examplebucket-1250000000.cos.ap-beijing.myqcloud.com/exampleobject
[ETag] => "7037fb6fb4cca43b958a28789605e73d98088720"
[ImageInfo] => Array
(
[Format] => JPEG
[Width] => 600
[Height] => 500
[Quality] => 90
[Ave] => 0x46442e
[Orientation] => 0
)

)
[ProcessResults] => Array
(
[Object] => Array
(
[0] => Array(
[Key] => resultobject
[Location] => examplebucket-1250000000.cos.ap-beijing.myqcloud.com/resultobject
[Format] => JPEG
[Width] => 300
[Height] => 200
[Size] => 30000
[Quality] => 90
[ETag] => "87c153bc2909aa0ba111ca126b675c510d36b817"
)
)
)
)
)


返回结果说明

参数名称
类型
描述
父节点
RequestId
String
请求 ID 标识
Key
String
对象键
Bucket
String
存储桶名称,格式:BucketName-APPID
Location
String
请求资源地址
OriginalInfo
Array
源图片信息
ProcessResults
Array
图片处理结果信息