|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +require dirname(__FILE__, 2) . '/vendor/autoload.php'; |
| 5 | + |
| 6 | +$secretId = "SECRETID"; //替换为用户的 secretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi |
| 7 | +$secretKey = "SECRETKEY"; //替换为用户的 secretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi |
| 8 | +$region = "ap-beijing"; //替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket |
| 9 | +$cosClient = new Qcloud\Cos\Client( |
| 10 | + array( |
| 11 | + 'region' => $region, |
| 12 | + 'scheme' => 'https', //协议头部,默认为http |
| 13 | + 'credentials' => array( |
| 14 | + 'secretId' => $secretId, |
| 15 | + 'secretKey' => $secretKey))); |
| 16 | +try { |
| 17 | + // 极智压缩 |
| 18 | + $imageRule = new Qcloud\Cos\ImageParamTemplate\ImageSlimTemplate(); |
| 19 | + $picOperations = new Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation(); |
| 20 | + $picOperations->setIsPicInfo(1); // is_pic_info |
| 21 | + $picOperations->addRule($imageRule, "output.png"); // rules |
| 22 | + |
| 23 | + // -------------------- 1. 下载时处理 -------------------- // |
| 24 | +// $downloadUrl = $cosClient->getObjectUrl('examplebucket-125000000', 'xxx.jpg'); // 获取下载链接 |
| 25 | + $downloadUrl = 'https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/xxx.jpg'; // 私有图片处理方式同上,仅增加签名部分,并与图片处理参数以“&”连接 |
| 26 | + $rule = $imageRule->queryString(); |
| 27 | + echo "{$downloadUrl}?{$rule}"; |
| 28 | +// echo "{$downloadUrl}&{$rule}"; // 携带签名的图片地址以“&”连接 |
| 29 | + // -------------------- 1. 下载时处理 -------------------- // |
| 30 | + |
| 31 | + // -------------------- 2. 上传时处理 -------------------- // |
| 32 | + $result = $cosClient->putObject(array( |
| 33 | + 'Bucket' => 'examplebucket-125000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket |
| 34 | + 'Key' => 'object.jpg', |
| 35 | + 'Body' => fopen('/tmp/local.jpg', 'rb'), // 本地文件 |
| 36 | + 'PicOperations' => $picOperations->queryString(), |
| 37 | + )); |
| 38 | + // 请求成功 |
| 39 | + print_r($result); |
| 40 | + // -------------------- 2. 上传时处理 -------------------- // |
| 41 | + |
| 42 | + // -------------------- 3. 云上数据处理 -------------------- // |
| 43 | + $result = $cosClient->ImageProcess(array( |
| 44 | + 'Bucket' => 'examplebucket-1250000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket |
| 45 | + 'Key' => 'test.png', |
| 46 | + 'PicOperations' => $picOperations->queryString(), |
| 47 | + )); |
| 48 | + // 请求成功 |
| 49 | + print_r($result); |
| 50 | + // -------------------- 3. 云上数据处理 -------------------- // |
| 51 | +} catch (\Exception $e) { |
| 52 | + // 请求失败 |
| 53 | + echo($e); |
| 54 | +} |
0 commit comments