PHP实现腾讯云cos对象储存盲水印添加获取文档
盲水印功能基于腾讯云数据万象,是一种全新的水印模式。通过该功能,您可将水印图以不可见的形式添加到原图信息中,并且不会对原图质量产生太大影响。当发现图片被盗取后,您可对疑似被盗取的资源进行盲水印提取,验证图片归属。
通过该 API 接口,您可以:
-
在上传时为图片添加盲水印
-
在下载时为图片添加盲水印
-
对 COS 上的图片添加盲水印
也可以对已添加盲水印的图片进行盲水印提取。
腾讯云cos-php-sdk
我们只需要下载或安装腾讯云cos-php-sdk,就可以很轻松的实现对对象储存进行上传下载以及一些常用的操作。荐使用 Composer 安装 cos-php-sdk-v5,Composer 是 PHP 的依赖管理工具,允许您声明项目所需的依赖,然后自动将它们安装到您的项目中。
在你的php项目目录下composer.json中的“require”节点下添加以下内容:
"require": { "qcloud/cos-sdk-v5": ">=2.0" }
然后执行composer更新命令:
composer update
添加盲水印
上传时添加
指定的水印图片必须同时满足如下3个条件:
-
1. 水印图片与源图片必须位于同一个存储桶下。
-
2. URL 需使用 COS 域名(不能使用 CDN 加速域名,例如 examplebucket-1250000000.file.myqcloud.com/shuiyin_2.png 不可用),且需保证水印图可访问。如果水印图读取权限为私有,则需要携带有效签名。
-
3. URL 必须以
http://
开始,不能省略 HTTP 头,也不能填 HTTPS 头,例如examplebucket-1250000000.cos.ap-shanghai.myqcloud.com/shuiyin_2.png
,https://examplebucket-1250000000.cos.ap-shanghai.myqcloud.com/shuiyin_2.png
为非法的水印 URL。
示例代码
<?php require dirname(__FILE__) . '/../vendor/autoload.php'; //如果使用了thinkphp、fasdadmin这类型的框架无需这行代码 $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, 'schema' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId , 'secretKey' => $secretKey))); try { $blindWatermarkTemplate = new Qcloud\Cos\ImageParamTemplate\BlindWatermarkTemplate();//创建盲水印参数模版实例 $blindWatermarkTemplate->setType(3);//盲水印类型,有效值:1 半盲;2 全盲;3 文字 $blindWatermarkTemplate->setImage("imageUrl");//设置盲水印图片地址 $blindWatermarkTemplate->setText("Test");//设置盲水印文字 $blindWatermarkTemplate->setLevel(3);//只对全盲水印(type=2)有效。level 的取值范围为{1,2,3},默认值为1,level 值越大则图片受影响程度越大、盲水印效果越好。 $picOperationsTemplate = new \Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation();//创建图片持久化处理参数模版实例 $picOperationsTemplate->setIsPicInfo(1);//设置是否返回原图信息,0不返回原图信息,1返回原图信息,默认为0 $picOperationsTemplate->addRule($blindWatermarkTemplate, "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 为对象键,对象键是对象在存储桶中的唯一标识。例如,在对象的访问域名 |
是 |
Body |
File/String |
上传的内容 |
是 |
PicOperations |
Json/String |
图片持久化处理信息 |
是 |
返回结果示例
Guzzle\Service\Resource\Model Object ( [structure:protected] => [data:protected] => Array ( [Body] => [ETag] => "698d51a19d8a121ce581499d7b701668" [RequestId] => NWQwOGRkNDdfMjJiMjU4NjRfNzVjXzEwNmVjY2M= [ContentLength] => 10000 [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 |
图片处理结果信息 |
无 |
下载时添加
指定的水印图片必须同时满足如下3个条件:
-
1. 水印图片与源图片必须位于同一个存储桶下。
-
2. URL 需使用 COS 域名(不能使用 CDN 加速域名,例如 examplebucket-1250000000.file.myqcloud.com/shuiyin_2.png 不可用),且需保证水印图可访问。如果水印图读取权限为私有,则需要携带有效签名。
-
3. URL 必须以
http://
开始,不能省略 HTTP 头,也不能填 HTTPS 头,例如examplebucket-1250000000.cos.ap-shanghai.myqcloud.com/shuiyin_2.png
,https://examplebucket-1250000000.cos.ap-shanghai.myqcloud.com/shuiyin_2.png
为非法的水印 URL。
示例代码
<?php require dirname(__FILE__) . '/../vendor/autoload.php'; //如果使用了thinkphp、fasdadmin这类型的框架无需这行代码 $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, 'schema' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId , 'secretKey' => $secretKey))); try { $blindWatermarkTemplate = new Qcloud\Cos\ImageParamTemplate\BlindWatermarkTemplate();//创建盲水印参数模版实例 $blindWatermarkTemplate->setType(3);//盲水印类型,有效值:1 半盲;2 全盲;3 文字 $blindWatermarkTemplate->setImage("imageUrl");//设置盲水印图片地址 $blindWatermarkTemplate->setText("Test");//设置盲水印文字 $blindWatermarkTemplate->setLevel(3);//只对全盲水印(type=2)有效。level 的取值范围为{1,2,3},默认值为1, $result = $cosClient->getObject(array( 'Bucket' => 'examplebucket-1250000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket 'Key' => 'exampleobject', 'ImageHandleParam' => $blindWatermarkTemplate->queryString(),//生成盲水印参数 )); // 请求成功 print_r($result); } catch (\Exception $e) { // 请求失败 echo($e); }
参数说明
参数名称 |
类型 |
描述 |
是否必填 |
Bucket |
String |
存储桶名称,格式:BucketName-APPID |
是 |
Key |
String |
此处的 Key 为对象键,对象键是对象在存储桶中的唯一标识。例如,在对象的访问域名 |
是 |
ImageHandleParam |
String |
盲水印参数 |
是 |
返回结果示例
Guzzle\Service\Resource\Model Object ( [structure:protected] => [data:protected] => Array ( [Body] => [RequestId] => NWQwOGRkNDdfMjJiMjU4NjRfNzVjXzEwNmVjY2M= [ContentLength] => 100 [CacheControl] => max-age=2592000 [ContentType] => image/jpeg [Key] => exampleobject [Bucket] => examplebucket-1250000000 [Location] => examplebucket-1250000000.cos.ap-beijing.myqcloud.com/exampleobject ) )
返回结果说明
参数名称 |
类型 |
描述 |
父节点 |
Body |
File/String |
下载内容 |
无 |
RequestId |
String |
请求 ID 标识 |
无 |
CacheControl |
String |
缓存策略,设置 Cache-Control |
无 |
ContentType |
String |
内容类型,设置 Content-Type |
无 |
ContentLength |
Int |
响应体长度 |
无 |
Key |
String |
对象键 |
无 |
Bucket |
String |
存储桶名称,格式:BucketName-APPID |
无 |
Location |
String |
请求资源地址 |
无 |
提取盲水印
图片地址,根据盲水印类型填写:
-
1.当半盲类型时,则盲水印图片地址必填,且为原图图片地址。
-
2.当全盲类型时,则盲水印图片地址必填,且为水印图地址。
-
3.当文字类型时,则盲水印图片地址无需填写(无效)。
指定的水印图片必须同时满足如下3个条件:
-
1. 水印图片与源图片必须位于同一个存储桶下。
-
2. URL 需使用 COS 域名(不能使用 CDN 加速域名,例如 examplebucket-1250000000.file.myqcloud.com/shuiyin_2.png 不可用),且需保证水印图可访问。如果水印图读取权限为私有,则需要携带有效签名。
-
3. URL 必须以
http://
开始,不能省略 HTTP 头,也不能填 HTTPS 头,例如examplebucket-1250000000.cos.ap-shanghai.myqcloud.com/shuiyin_2.png
,https://examplebucket-1250000000.cos.ap-shanghai.myqcloud.com/shuiyin_2.png
为非法的水印 URL。
示例代码
<?php require dirname(__FILE__) . '/../vendor/autoload.php'; //如果使用了thinkphp、fasdadmin这类型的框架无需这行代码 $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, 'schema' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId , 'secretKey' => $secretKey))); try { $blindWatermarkTemplate = new Qcloud\Cos\ImageParamTemplate\BlindWatermarkTemplate();//创建盲水印参数模版实例 $blindWatermarkTemplate->setPick();//设置为提取盲水印 $blindWatermarkTemplate->setType(3);//盲水印类型,有效值:1 半盲;2 全盲;3 文字 $blindWatermarkTemplate->setImage("imageUrl");//设置盲水印图片地址 $blindWatermarkTemplate->setText("Test");//设置盲水印文字 $blindWatermarkTemplate->setLevel(3);//只对全盲水印(type=2)有效。level 的取值范围为{1,2,3},默认值为1,level 值越大则图片受影响程度越大、盲水印效果越好。 $picOperationsTemplate = new \Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation();//创建图片持久化处理参数模版实例 $picOperationsTemplate->setIsPicInfo(1);//设置是否返回原图信息,0不返回原图信息,1返回原图信息,默认为0 $picOperationsTemplate->addRule($blindWatermarkTemplate, "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 为对象键,对象键是对象在存储桶中的唯一标识。例如,在对象的访问域名 |
是 |
Body |
File/String |
上传的内容 |
是 |
PicOperations |
Json/String |
图片持久化处理信息 |
是 |
返回结果示例
Guzzle\Service\Resource\Model Object ( [structure:protected] => [data:protected] => Array ( [Body] => [ETag] => "698d51a19d8a121ce581499d7b701668" [RequestId] => NWQwOGRkNDdfMjJiMjU4NjRfNzVjXzEwNmVjY2M= [ContentLength] => 10000 [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 [WatermarkStatus] => 90 [ETag] => "87c153bc2909aa0ba111ca126b675c510d36b817" ) ) ) ) ) )
返回结果说明
参数名称 |
类型 |
描述 |
父节点 |
Body |
File/String |
返回体 |
无 |
ETag |
String |
文件的 MD5 值 |
无 |
RequestId |
String |
请求 ID 标识 |
无 |
ContentLength |
Int |
响应体长度 |
无 |
Key |
String |
对象键 |
无 |
Bucket |
String |
存储桶名称,格式:BucketName-APPID |
无 |
Location |
String |
请求资源地址 |
无 |
Data |
Array |
图片处理结果信息 |
无 |
本文链接:https://yun.ha.cn/post/148.html 转载需授权!