imageRepairProcess.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. require dirname(__FILE__, 2) . '/vendor/autoload.php';
  3. $secretId = "SECRETID"; //替换为用户的 secretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi
  4. $secretKey = "SECRETKEY"; //替换为用户的 secretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi
  5. $region = "ap-beijing"; //替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket
  6. $cosClient = new Qcloud\Cos\Client(
  7. array(
  8. 'region' => $region,
  9. 'schema' => 'https', //协议头部,默认为http
  10. 'credentials'=> array(
  11. 'secretId' => $secretId ,
  12. 'secretKey' => $secretKey)));
  13. try {
  14. // --------------------- 1. 保存效果图到本地 ------------------------------ //
  15. $imageUrl = 'https://www.xxx.com/xxx.jpg';
  16. $result = $cosClient->imageRepairProcess(array(
  17. 'Bucket' => 'examplebucket-1250000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
  18. 'Key' => 'test.jpg',
  19. 'ci-process' => 'ImageRepair',
  20. 'MaskPic' => base64_encode($imageUrl),
  21. // 'MaskPoly' => base64_encode('[[[200, 200], [400, 200], [400, 400], [200, 400]]]'),
  22. 'SaveAs' => '/tmp/imageRepair.jpg' // 本地保存路径
  23. ));
  24. // 请求成功
  25. print_r($result);
  26. // --------------------- 1. 保存效果图到本地 ------------------------------ //
  27. // --------------------- 2. 上传时处理 ------------------------------ //
  28. $ciProcessParams = new Qcloud\Cos\ImageParamTemplate\CIProcessTransformation('ImageRepair');
  29. $ciProcessParams->addParam('MaskPic', 'https://www.xxx.com/xxx.jpg', true); // MaskPic/MaskPoly 二选一
  30. // $ciProcessParams->addParam('MaskPoly', '[[[200, 200], [400, 200], [400, 400], [200, 400]]]', true); // MaskPic/MaskPoly 二选一
  31. $picOperations = new Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation();
  32. $picOperations->setIsPicInfo(1); // is_pic_info
  33. $picOperations->addRule($ciProcessParams, 'output.jpg', 'examplebucket-1250000000'); // rules
  34. $result = $cosClient->putObject(array(
  35. 'Bucket' => 'examplebucket-1250000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
  36. 'Key' => 'imageRepair.jpg',
  37. 'Body' => fopen('/tmp/imageRepair.jpg', 'rb'), // 本地文件
  38. 'PicOperations' => $picOperations->queryString(),
  39. ));
  40. // 请求成功
  41. print_r($result);
  42. // --------------------- 2. 上传时处理 ------------------------------ //
  43. // --------------------- 3. 云上数据处理 ------------------------------ //
  44. $ciProcessParams = new Qcloud\Cos\ImageParamTemplate\CIProcessTransformation('ImageRepair');
  45. $ciProcessParams->addParam('MaskPic', 'https://www.xxx.com/xxx.jpg', true); // MaskPic/MaskPoly 二选一
  46. // $ciProcessParams->addParam('MaskPoly', '[[[200, 200], [400, 200], [400, 400], [200, 400]]]', true); // MaskPic/MaskPoly 二选一
  47. $picOperations = new Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation();
  48. $picOperations->setIsPicInfo(1); // is_pic_info
  49. $picOperations->addRule($ciProcessParams, 'output.jpg', 'examplebucket-1250000000'); // rules
  50. $result = $cosClient->ImageProcess(array(
  51. 'Bucket' => 'examplebucket-1250000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
  52. 'Key' => 'test.jpg',
  53. 'PicOperations' => $picOperations->queryString(),
  54. ));
  55. // 请求成功
  56. print_r($result);
  57. // --------------------- 3. 云上数据处理 ------------------------------ //
  58. } catch (\Exception $e) {
  59. // 请求失败
  60. echo($e);
  61. }