php-sdk-腾讯云文字盲水印添加/提取

起因:项目需求方需要对上传的图片添加盲水印

SDK文档链接:https://cloud.tencent.com/document/product/436/55584

直接看SDK的示例代码:

代码语言:javascript
复制
<?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,
'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-&gt;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);
}

一开始跑这个示例的时候,是很奇怪的。原因有以下几点:

1、为何要指定Key,按理来说添加盲水印都是未上传的图片?

2、上传完成后,有个resultobject文件,添加了水印后的图片是哪个?是key么?

3、返回的信息,如何判断盲水印是否成功添加?

带着疑惑各种百度,都找不到问题的答案。直到去翻了下python的sdk。链接:https://cloud.tencent.com/document/product/436/55346

好家伙,原来resultobject是添加盲水印后生成的文件。

继续修改:最终代码:

代码语言:javascript
复制
<?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,
'schema' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));

try {
$blindWatermarkTemplate = new Qcloud\Cos\ImageParamTemplate\BlindWatermarkTemplate();//创建盲水印参数模版实例
$blindWatermarkTemplate->setType(3);//盲水印类型,有效值:1 半盲;2 全盲;3 文字
$blindWatermarkTemplate->setText("Test");//设置盲水印文字
$picOperationsTemplate = new \Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation();//创建图片持久化处理参数模版实例
picOperationsTemplate-&gt;addRule(blindWatermarkTemplate, "format.jpg");//设置图片持久化处理参数
result = cosClient->putObject(array(
'Bucket' => 'examplebucket-1250000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
'Key' => 'file.jpg',
'Body' => fopen('path/to/file.jpg', 'rb'),
'PicOperations' => $picOperationsTemplate->queryString(),//生成图片持久化处理参数
));
// 请求成功
print_r($result);
} catch (\Exception $e) {
// 请求失败
echo($e);
}

以上是文字盲水印的代码,因为文字盲水印不需要水印图,所以不需要setImage,setLevel这些参数。

上面的3个问题:

1、key是添加水印的原图

2、resultobject是添加水印后生成的新文件,因为文档里没有明确说明,所以很容易误导

3、最简单的判断方法是看文件大小,如果format.jpg比file.jpg大,则说明水印添加成功

再附上提取代码

代码语言:javascript
复制
<?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,
'schema' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));

try {
$blindWatermarkTemplate = new Qcloud\Cos\ImageParamTemplate\BlindWatermarkTemplate();//创建盲水印参数模版实例
$blindWatermarkTemplate->setPick();//设置为提取盲水印
$blindWatermarkTemplate->setType(3);//盲水印类型,有效值:1 半盲;2 全盲;3 文字
$picOperationsTemplate = new \Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation();//创建图片持久化处理参数模版实例
picOperationsTemplate-&gt;addRule(blindWatermarkTemplate, "watermark.jpg");//设置图片持久化处理参数
result = cosClient->putObject(array(
'Bucket' => 'examplebucket-1250000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
'Key' => 'format.jpg',
'Body' => fopen('path/to/localFile', 'rb'),
'PicOperations' => $picOperationsTemplate->queryString(),//生成图片持久化处理参数
));
// 请求成功
print_r($result);
} catch (\Exception $e) {
// 请求失败
echo($e);
}

format.jpg是添加了水印之后的图片,watermark.jpg是提取的水印文字图。

watermark.jpg