控制台指南

最佳实践

开发者指南

数据湖存储

API 文档

SDK 文档

文档捉虫大赛火热进行中,好礼多多> HOT

简介

腾讯云对象存储(Cloud Object Storage,COS)集成了 数据万象(Cloud Infinite,CI)专业的一体化多媒体解决方案,涵盖以下图片处理功能,详情可见 图片处理概述

功能 说明
缩放 等比缩放、设定目标宽高缩放等多种方式
裁剪 普通裁剪、缩放裁剪、内切圆、人脸智能裁剪
旋转 自适应旋转、普通旋转
格式转换 格式转换、GIF 格式优化、渐进显示
质量变换 针对 JPG 和 WEBP 图片进行质量变换
高斯模糊 对图片进行模糊处理
亮度 调节图片亮度
对比度 调节图片对比度
锐化 对图片进行锐化处理
灰度图 将图片设置为灰度图
图片水印 提供图片水印处理功能
文字水印 提供文字水印处理功能
获取图片基本信息 查询图片基本信息,包括格式、长、宽等
获取图片 EXIF 获取 EXIF 信息
获取图片主色调 获取图片主色调信息
去除元信息 可去除图片元信息,包括 exif 信息
快速缩略模板 快速实现图片格式转换、缩略、剪裁等功能,生成缩略图
限制图片大小 可限制图片处理(例如缩放、压缩等)后的文件大小
管道操作符 实现对图片按顺序进行多种处理

缩放

功能说明

腾讯云数据万象通过 imageMogr2 接口提供图片缩放功能。处理图片原图大小不超过32MB、宽高不超过30000像素且总像素不超过2.5亿像素,处理结果图宽高设置不超过9999像素;针对动图,原图宽 x 高 x 帧数不超过2.5亿像素。

代码示例

<?php

require dirname(__FILE__, 2) . '/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 {
    // 图片处理-缩放 https://cloud.tencent.com/document/product/460/36540
    $imageRule = new Qcloud\Cos\ImageParamTemplate\ImageMogrTemplate();
    $imageRule->thumbnailByScale(50); // /thumbnail/!<Scale>p
    $imageRule->thumbnailByWidthScale(50); // /thumbnail/!<Scale>px
    $imageRule->thumbnailByHeightScale(50); // /thumbnail/!x<Scale>p
    $imageRule->thumbnailByWidth(60); // /thumbnail/<Width>x
    $imageRule->thumbnailByHeight(60); // /thumbnail/x<Height>
    $imageRule->thumbnailByMaxWH(100, 100); // /thumbnail/<Width>x<Height>
    $imageRule->thumbnailByMinWH(30, 30); // /thumbnail/!<Width>x<Height>r
    $imageRule->thumbnailByWH(50, 50); // /thumbnail/<Width>x<Height>!
    $imageRule->thumbnailByPixel(10000); // /thumbnail/<Area>@
    $imageRule->thumbnailEqualRatioReduceByWH(300, 300); // /thumbnail/<Width>x<Height>>
    $imageRule->thumbnailEqualRatioEnlargeByWH(300, 300); // /thumbnail/<Width>x<Height><
    $imageRule->pad(1); // /pad/1
    $imageRule->color('#3D3D3D'); // /color/IzNEM0QzRA
    $imageRule->ignoreError(); // /ignore-error/1

    $picOperations = new Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation();
    $picOperations->setIsPicInfo(1); // is_pic_info
    $picOperations->addRule($imageRule, "output.png"); // rules

    // -------------------- 1. 下载时处理 -------------------- //
//    $downloadUrl = $cosClient->getObjectUrl('examplebucket-1250000000', 'xxx.jpg'); // 获取下载链接
    $downloadUrl = 'https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/xxx.jpg'; // 私有图片处理方式同上,仅增加签名部分,并与图片处理参数以“&”连接
    $rule = $imageRule->queryString();
    echo "{$downloadUrl}?{$rule}";
//    echo "{$downloadUrl}&{$rule}"; // 携带签名的图片地址以“&”连接
    // -------------------- 1. 下载时处理 -------------------- //

    // -------------------- 2. 上传时处理 -------------------- //
    $result = $cosClient->putObject(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'object.jpg',
        'Body' => fopen('/tmp/local.jpg', 'rb'), // 本地文件
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 2. 上传时处理 -------------------- //

    // -------------------- 3. 云上数据处理 -------------------- //
    $result = $cosClient->ImageProcess(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'test.png',
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 3. 云上数据处理 -------------------- //
} catch (\Exception $e) {
    // 请求失败
    echo($e);
}

处理参数说明

操作名称:thumbnail。

参数 含义
ObjectKey 对象文件名,例如 folder/sample.jpg。
/thumbnail/!<Scale>p 指定图片的宽高为原图的 Scale%
/thumbnail/!<Scale>px 指定图片的宽为原图的 Scale%,高度不变
/thumbnail/!x<Scale>p 指定图片的高为原图的 Scale%,宽度不变
/thumbnail/<Width>x 指定目标图片宽度为 Width,高度等比缩放
/thumbnail/x<Height> 指定目标图片高度为 Height,宽度等比缩放
/thumbnail/<Width>x<Height> 限定缩略图的宽度和高度的最大值分别为 Width 和 Height,进行等比缩放
/thumbnail/<Width>x<Height>> 限定缩略图的宽度和高度的最大值分别为 Width 和 Height,进行等比缩小,比例值为宽缩放比和高缩放比的较小值,如果目标宽(高)都大于原图宽(高),则不变
/thumbnail/<Width>x<Height>< 限定缩略图的宽度和高度的最大值分别为 Width 和 Height,进行等比放大,比例值为宽缩放比和高缩放比的较小值。如果目标宽(高)小于原图宽(高),则不变
/thumbnail/!<Width>x<Height>r 限定缩略图的宽度和高度的最小值分别为 Width 和 Height,进行等比缩放
/thumbnail/<Width>x<Height>! 忽略原图宽高比例,指定图片宽度为 Width,高度为 Height,强行缩放图片,可能导致目标图片变形
/thumbnail/<Area>@ 等比缩放图片,缩放后的图像,总像素数量不超过 Area
/pad/ 将原图缩放为指定 Width 和 Height 的矩形内的最大图片,之后使用 color 参数指定的颜色居中填充空白部分;取值0或1,0代表不使用 pad 模式,1代表使用 pad 模式
/color/ 填充颜色,缺省为白色,需设置为十六进制 RGB 格式(如 #FF0000),详情参见 RGB 编码表,需经过 URL 安全的 Base64 编码,默认值为 #FFFFFF
/ignore-error/1 当处理参数中携带此参数时,针对文件过大、参数超限等导致处理失败的场景,会直接返回原图而不报错

裁剪

功能说明

腾讯云数据万象通过 imageMogr2 接口提供裁剪功能,包括普通裁剪、缩放裁剪、内切圆裁剪、圆角裁剪和人脸智能裁剪。

代码示例

<?php

require dirname(__FILE__, 2) . '/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 {
    // 图片处理-裁剪 https://cloud.tencent.com/document/product/460/36541
    $imageRule = new Qcloud\Cos\ImageParamTemplate\ImageMogrTemplate();
    $imageRule->cut(400, 400, 100, 100); // 普通裁剪参数 /cut/<width>x<height>x<dx>x<dy>
    $imageRule->cropByWidth(400, 'center'); // 缩放裁剪参数 /crop/<Width>x
    $imageRule->cropByHeight(400, 'center'); // 缩放裁剪参数 /crop/x<Height>
    $imageRule->cropByWH(400, 400, 'center'); // 缩放裁剪参数 /crop/<Width>x<Height>
    $imageRule->iradius(30); // 内切圆裁剪参数 /iradius/<radius>
    $imageRule->rradius(30); // 圆角裁剪参数 /rradius/<radius>
    $imageRule->scrop(30, 30); // 人脸智能裁剪参数 /scrop/<Width>x<Height>
    $imageRule->ignoreError(); // /ignore-error/1

    $picOperations = new Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation();
    $picOperations->setIsPicInfo(1); // is_pic_info
    $picOperations->addRule($imageRule, "output.png"); // rules

    // -------------------- 1. 下载时处理 -------------------- //
//    $downloadUrl = $cosClient->getObjectUrl('examplebucket-1250000000', 'xxx.jpg'); // 获取下载链接
    $downloadUrl = 'https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/xxx.jpg'; // 私有图片处理方式同上,仅增加签名部分,并与图片处理参数以“&”连接
    $rule = $imageRule->queryString();
    echo "{$downloadUrl}?{$rule}";
//    echo "{$downloadUrl}&{$rule}"; // 携带签名的图片地址以“&”连接
    // -------------------- 1. 下载时处理 -------------------- //

    // -------------------- 2. 上传时处理 -------------------- //
    $result = $cosClient->putObject(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'object.jpg',
        'Body' => fopen('/tmp/local.jpg', 'rb'), // 本地文件
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 2. 上传时处理 -------------------- //

    // -------------------- 3. 云上数据处理 -------------------- //
    $result = $cosClient->ImageProcess(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'test.png',
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 3. 云上数据处理 -------------------- //
} catch (\Exception $e) {
    // 请求失败
    echo($e);
}

处理参数说明

参数 含义
/ignore-error/1 当处理参数中携带此参数时,针对文件过大、参数超限等导致处理失败的场景,会直接返回原图而不报错
普通裁剪参数说明

操作名称:cut。

参数 含义
<width> 指定目标图片的宽为 width
<height> 指定目标图片的高为 height
<dx> 相对于图片左上顶点水平向右偏移 dx
<dy> 相对于图片左上顶点水平向下偏移 dy
说明:

参数取值范围应大于0,小于原图宽高。

缩放裁剪参数说明

操作名称:crop。

参数 含义
/crop/<Width>x 指定目标图片宽度为 Width,高度不变。Width 取值范围应大于0,小于原图宽度
/crop/x<Height> 指定目标图片高度为 Height,宽度不变。Height 取值范围应大于0,小于原图高度
/crop/<Width>x<Height> 指定目标图片宽度为 Width,高度为 Height 。Width 和 Height 取值范围都应大于0,小于原图宽度/高度

在进行缩放裁剪操作时,您也可以使用 gravity 参数指定操作的起点位置,详情请参见下文中 缩放裁剪操作示例

内切圆裁剪参数说明

操作名称:iradius。

参数 含义
/iradius/<radius> 内切圆裁剪功能,radius 是内切圆的半径,取值范围为大于0且小于原图最小边一半的整数。内切圆的圆心为图片的中心。图片格式为 gif 时,不支持该参数。
圆角裁剪参数说明

操作名称:rradius。

参数 含义
/rradius/<radius> 圆角裁剪功能,radius 为图片圆角边缘的半径,取值范围为大于0且小于原图最小边一半的整数。圆角与原图边缘相切。图片格式为 gif 时,不支持该参数。
人脸智能裁剪参数说明

操作名称:scrop。

参数 含义
/scrop/<Width>x<Height> 基于图片中的人脸位置进行缩放裁剪。目标图片的宽度为 Width、高度为 Height。

旋转

功能说明

腾讯云数据万象通过 imageMogr2 接口提供旋转功能,包括普通旋转和自适应旋转。

代码示例

<?php

require dirname(__FILE__, 2) . '/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 {
    // 图片处理-旋转 https://cloud.tencent.com/document/product/460/36542
    $imageRule = new Qcloud\Cos\ImageParamTemplate\ImageMogrTemplate();
    $imageRule->rotate(90); // /rotate/<rotateDegree>
    $imageRule->autoOrient(); // /auto-orient
    $imageRule->flip('vertical'); // /flip/<flip>
    $imageRule->ignoreError(); // /ignore-error/1

    $picOperations = new Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation();
    $picOperations->setIsPicInfo(1); // is_pic_info
    $picOperations->addRule($imageRule, "output.png"); // rules

    // -------------------- 1. 下载时处理 -------------------- //
//    $downloadUrl = $cosClient->getObjectUrl('examplebucket-1250000000', 'xxx.jpg'); // 获取下载链接
    $downloadUrl = 'https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/xxx.jpg'; // 私有图片处理方式同上,仅增加签名部分,并与图片处理参数以“&”连接
    $rule = $imageRule->queryString();
    echo "{$downloadUrl}?{$rule}";
//    echo "{$downloadUrl}&{$rule}"; // 携带签名的图片地址以“&”连接
    // -------------------- 1. 下载时处理 -------------------- //

    // -------------------- 2. 上传时处理 -------------------- //
    $result = $cosClient->putObject(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'object.jpg',
        'Body' => fopen('/tmp/local.jpg', 'rb'), // 本地文件
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 2. 上传时处理 -------------------- //

    // -------------------- 3. 云上数据处理 -------------------- //
    $result = $cosClient->ImageProcess(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'test.png',
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 3. 云上数据处理 -------------------- //
} catch (\Exception $e) {
    // 请求失败
    echo($e);
}

处理参数说明

参数 含义
/rotate/<rotateDegree> 普通旋转:图片顺时针旋转角度,取值范围0 - 360,默认不旋转。
/auto-orient 自适应旋转:根据原图 EXIF 信息将图片自适应旋转回正。
/flip/<flip> 镜像翻转:flip 值为 vertical 表示垂直翻转,horizontal 表示水平翻转
/ignore-error/1 当处理参数中携带此参数时,针对文件过大、参数超限等导致处理失败的场景,会直接返回原图而不报错

格式转换

功能说明

腾讯云数据万象通过 imageMogr2 接口提供格式转换、gif 格式优化、渐进显示功能。

代码示例

<?php

require dirname(__FILE__, 2) . '/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 {
    // 图片处理-格式转换 https://cloud.tencent.com/document/product/460/36543
    $imageRule = new Qcloud\Cos\ImageParamTemplate\ImageMogrTemplate();
    $imageRule->format('jpg'); // 格式转换 /format/<Format>
    $imageRule->gifOptimization(1); // gif 格式优化 /cgif/<FrameNumber>
    $imageRule->jpegInterlaceMode(1); // 输出为渐进式 jpg 格式。Mode 可为0或1 /interlace/<Mode>
    $imageRule->ignoreError(); // /ignore-error/1

    $picOperations = new Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation();
    $picOperations->setIsPicInfo(1); // is_pic_info
    $picOperations->addRule($imageRule, "output.png"); // rules

    // -------------------- 1. 下载时处理 -------------------- //
//    $downloadUrl = $cosClient->getObjectUrl('examplebucket-1250000000', 'xxx.jpg'); // 获取下载链接
    $downloadUrl = 'https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/xxx.jpg'; // 私有图片处理方式同上,仅增加签名部分,并与图片处理参数以“&”连接
    $rule = $imageRule->queryString();
    echo "{$downloadUrl}?{$rule}";
//    echo "{$downloadUrl}&{$rule}"; // 携带签名的图片地址以“&”连接
    // -------------------- 1. 下载时处理 -------------------- //

    // -------------------- 2. 上传时处理 -------------------- //
    $result = $cosClient->putObject(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'object.jpg',
        'Body' => fopen('/tmp/local.jpg', 'rb'), // 本地文件
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 2. 上传时处理 -------------------- //

    // -------------------- 3. 云上数据处理 -------------------- //
    $result = $cosClient->ImageProcess(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'test.png',
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 3. 云上数据处理 -------------------- //
} catch (\Exception $e) {
    // 请求失败
    echo($e);
}

处理参数说明

参数 含义
/format/<Format> 格式转换:目标缩略图的图片格式可为:jpg,bmp,gif,png,webp,yjpeg 等,其中 yjpeg 为数据万象针对 jpeg 格式进行的优化,本质为 jpg 格式;缺省为原图格式。
/cgif/<FrameNumber> gif 格式优化:只针对原图为 gif 格式,对 gif 图片格式进行的优化,降帧降颜色。分为以下两种情况:FrameNumber=1,则按照默认帧数30处理,如果图片帧数大于该帧数则截取。FrameNumber 取值( 1,100 ],则将图片压缩到指定帧数 (FrameNumber)。
/interlace/<Mode> 输出为渐进式 jpg 格式。Mode 可为0或1。0:表示不开启渐进式;1:表示开启渐进式。该参数仅在输出图片格式为 jpg 格式时有效。如果输出非 jpg 图片格式,会忽略该参数,默认值0。
/ignore-error/1 当处理参数中携带此参数时,针对文件过大、参数超限等导致处理失败的场景,会直接返回原图而不报错

质量变换

功能说明

腾讯云数据万象通过 imageMogr2 接口对图片质量进行调节。

代码示例

<?php

require dirname(__FILE__, 2) . '/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 {
    // 图片处理-质量变换 https://cloud.tencent.com/document/product/460/36544
    $imageRule = new Qcloud\Cos\ImageParamTemplate\ImageMogrTemplate();
    $imageRule->quality(90, 1); // 绝对质量 /quality/<Quality>
    $imageRule->lowestQuality(90); // 最低质量 /lquality/<quality>
    $imageRule->relativelyQuality(90); // 相对质量 /rquality/<quality>
    $imageRule->ignoreError(); // /ignore-error/1

    $picOperations = new Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation();
    $picOperations->setIsPicInfo(1); // is_pic_info
    $picOperations->addRule($imageRule, "output.png"); // rules

    // -------------------- 1. 下载时处理 -------------------- //
//    $downloadUrl = $cosClient->getObjectUrl('examplebucket-1250000000', 'xxx.jpg'); // 获取下载链接
    $downloadUrl = 'https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/xxx.jpg'; // 私有图片处理方式同上,仅增加签名部分,并与图片处理参数以“&”连接
    $rule = $imageRule->queryString();
    echo "{$downloadUrl}?{$rule}";
//    echo "{$downloadUrl}&{$rule}"; // 携带签名的图片地址以“&”连接
    // -------------------- 1. 下载时处理 -------------------- //

    // -------------------- 2. 上传时处理 -------------------- //
    $result = $cosClient->putObject(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'object.jpg',
        'Body' => fopen('/tmp/local.jpg', 'rb'), // 本地文件
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 2. 上传时处理 -------------------- //

    // -------------------- 3. 云上数据处理 -------------------- //
    $result = $cosClient->ImageProcess(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'test.png',
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 3. 云上数据处理 -------------------- //
} catch (\Exception $e) {
    // 请求失败
    echo($e);
}

处理参数说明

操作名称:quality。

参数 含义
/quality/<Quality> 图片的绝对质量,取值范围0 - 100,默认值为原图质量;取原图质量和指定质量的最小值;后面加“!”表示强制使用指定值,例如:90!
/rquality/<quality> 图片的相对质量,取值范围0 - 100,数值以原图质量为标准。例如原图质量为80,将 rquality 设置为80后,得到处理结果图的图片质量为64(80x80%)。
/lquality/<quality> 图片的最低质量,取值范围0 - 100,设置结果图的质量参数最小值。 例如原图质量为85,将 lquality 设置为80后,处理结果图的图片质量为85。例如原图质量为60,将 lquality 设置为80后,处理结果图的图片质量会被提升至80。
/ignore-error/1 当处理参数中携带此参数时,针对文件过大、参数超限等导致处理失败的场景,会直接返回原图而不报错

高斯模糊

功能说明

腾讯云数据万象通过 imageMogr2 接口对图片进行模糊处理。

代码示例

<?php

require dirname(__FILE__, 2) . '/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 {
    // 图片处理-高斯模糊 https://cloud.tencent.com/document/product/460/36545
    $imageRule = new Qcloud\Cos\ImageParamTemplate\ImageMogrTemplate();
    $imageRule->blur(8, 5); // 高斯模糊 /blur/<radius>x<sigma>
    $imageRule->ignoreError(); // /ignore-error/1

    $picOperations = new Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation();
    $picOperations->setIsPicInfo(1); // is_pic_info
    $picOperations->addRule($imageRule, "output.png"); // rules

    // -------------------- 1. 下载时处理 -------------------- //
//    $downloadUrl = $cosClient->getObjectUrl('examplebucket-1250000000', 'xxx.jpg'); // 获取下载链接
    $downloadUrl = 'https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/xxx.jpg'; // 私有图片处理方式同上,仅增加签名部分,并与图片处理参数以“&”连接
    $rule = $imageRule->queryString();
    echo "{$downloadUrl}?{$rule}";
//    echo "{$downloadUrl}&{$rule}"; // 携带签名的图片地址以“&”连接
    // -------------------- 1. 下载时处理 -------------------- //

    // -------------------- 2. 上传时处理 -------------------- //
    $result = $cosClient->putObject(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'object.jpg',
        'Body' => fopen('/tmp/local.jpg', 'rb'), // 本地文件
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 2. 上传时处理 -------------------- //

    // -------------------- 3. 云上数据处理 -------------------- //
    $result = $cosClient->ImageProcess(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'test.png',
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 3. 云上数据处理 -------------------- //
} catch (\Exception $e) {
    // 请求失败
    echo($e);
}

处理参数说明

操作名称:blur。

参数 含义
radius<radius> 模糊半径,取值范围为1 - 50
sigma<sigma> 正态分布的标准差,必须大于0
/ignore-error/1 当处理参数中携带此参数时,针对文件过大、参数超限等导致处理失败的场景,会直接返回原图而不报错

亮度

功能说明

腾讯云数据万象通过 imageMogr2 接口调节图片亮度。

代码示例

<?php


require dirname(__FILE__, 2) . '/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 {
    // 图片处理-亮度 https://cloud.tencent.com/document/product/460/51808
    $imageRule = new Qcloud\Cos\ImageParamTemplate\ImageMogrTemplate();
    $imageRule->bright(70); // 图片亮度调节 /bright/<value>
    $imageRule->ignoreError(); // /ignore-error/1

    $picOperations = new Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation();
    $picOperations->setIsPicInfo(1); // is_pic_info
    $picOperations->addRule($imageRule, "output.png"); // rules

    // -------------------- 1. 下载时处理 -------------------- //
//    $downloadUrl = $cosClient->getObjectUrl('examplebucket-1250000000', 'xxx.jpg'); // 获取下载链接
    $downloadUrl = 'https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/xxx.jpg'; // 私有图片处理方式同上,仅增加签名部分,并与图片处理参数以“&”连接
    $rule = $imageRule->queryString();
    echo "{$downloadUrl}?{$rule}";
//    echo "{$downloadUrl}&{$rule}"; // 携带签名的图片地址以“&”连接
    // -------------------- 1. 下载时处理 -------------------- //

    // -------------------- 2. 上传时处理 -------------------- //
    $result = $cosClient->putObject(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'object.jpg',
        'Body' => fopen('/tmp/local.jpg', 'rb'), // 本地文件
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 2. 上传时处理 -------------------- //

    // -------------------- 3. 云上数据处理 -------------------- //
    $result = $cosClient->ImageProcess(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'test.png',
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 3. 云上数据处理 -------------------- //
} catch (\Exception $e) {
    // 请求失败
    echo($e);
}

处理参数说明

操作名称:bright。

参数 含义
/bright/<value> 图片亮度调节功能,value 为亮度参数值,取值范围为[-100, 100]的整数。 取值<0:降低图片亮度。取值 = 0:不调整图片亮度。取值>0:提高图片亮度。
/ignore-error/1 当处理参数中携带此参数时,针对文件过大、参数超限等导致处理失败的场景,会直接返回原图而不报错。

对比度

功能说明

对比度指一幅图像中明暗区域最亮的白和最暗的黑之间不同亮度层级的测量,即指一幅图像灰度反差的大小。腾讯云数据万象通过 imageMogr2 接口调节图片对比度。

代码示例

<?php


require dirname(__FILE__, 2) . '/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 {
    // 图片处理-对比度 https://cloud.tencent.com/document/product/460/51809
    $imageRule = new Qcloud\Cos\ImageParamTemplate\ImageMogrTemplate();
    $imageRule->contrast(-70); // 图片对比度调节 /contrast/<value>
    $imageRule->ignoreError(); // /ignore-error/1

    $picOperations = new Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation();
    $picOperations->setIsPicInfo(1); // is_pic_info
    $picOperations->addRule($imageRule, "output.png"); // rules

    // -------------------- 1. 下载时处理 -------------------- //
//    $downloadUrl = $cosClient->getObjectUrl('examplebucket-1250000000', 'xxx.jpg'); // 获取下载链接
    $downloadUrl = 'https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/xxx.jpg'; // 私有图片处理方式同上,仅增加签名部分,并与图片处理参数以“&”连接
    $rule = $imageRule->queryString();
    echo "{$downloadUrl}?{$rule}";
//    echo "{$downloadUrl}&{$rule}"; // 携带签名的图片地址以“&”连接
    // -------------------- 1. 下载时处理 -------------------- //

    // -------------------- 2. 上传时处理 -------------------- //
    $result = $cosClient->putObject(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'object.jpg',
        'Body' => fopen('/tmp/local.jpg', 'rb'), // 本地文件
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 2. 上传时处理 -------------------- //

    // -------------------- 3. 云上数据处理 -------------------- //
    $result = $cosClient->ImageProcess(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'test.png',
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 3. 云上数据处理 -------------------- //
} catch (\Exception $e) {
    // 请求失败
    echo($e);
}

处理参数说明

操作名称:contrast。

参数 含义
/contrast/<value> 图片对比度调节功能,value 为对比度参数值,取值范围为[-100, 100]的整数。 取值<0:降低图片对比度。取值 = 0:不调整图片对比度。取值>0:提高图片对比度。
/ignore-error/1 当处理参数中携带此参数时,针对文件过大、参数超限等导致处理失败的场景,会直接返回原图而不报错。

锐化

功能说明

腾讯云数据万象通过 imageMogr2 接口对图片进行锐化处理。

代码示例

<?php


require dirname(__FILE__, 2) . '/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 {
    // 图片处理-锐化 https://cloud.tencent.com/document/product/460/36546
    $imageRule = new Qcloud\Cos\ImageParamTemplate\ImageMogrTemplate();
    $imageRule->sharpen(70); // 图片锐化 /sharpen/<value>
    $imageRule->ignoreError(); // /ignore-error/1

    $picOperations = new Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation();
    $picOperations->setIsPicInfo(1); // is_pic_info
    $picOperations->addRule($imageRule, "output.png"); // rules

    // -------------------- 1. 下载时处理 -------------------- //
//    $downloadUrl = $cosClient->getObjectUrl('examplebucket-1250000000', 'xxx.jpg'); // 获取下载链接
    $downloadUrl = 'https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/xxx.jpg'; // 私有图片处理方式同上,仅增加签名部分,并与图片处理参数以“&”连接
    $rule = $imageRule->queryString();
    echo "{$downloadUrl}?{$rule}";
//    echo "{$downloadUrl}&{$rule}"; // 携带签名的图片地址以“&”连接
    // -------------------- 1. 下载时处理 -------------------- //

    // -------------------- 2. 上传时处理 -------------------- //
    $result = $cosClient->putObject(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'object.jpg',
        'Body' => fopen('/tmp/local.jpg', 'rb'), // 本地文件
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 2. 上传时处理 -------------------- //

    // -------------------- 3. 云上数据处理 -------------------- //
    $result = $cosClient->ImageProcess(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'test.png',
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 3. 云上数据处理 -------------------- //
} catch (\Exception $e) {
    // 请求失败
    echo($e);
}

处理参数说明

操作名称:sharpen。

参数 含义
/sharpen/<value> 图片锐化功能,value 为锐化参数值,取值范围为10 - 300间的整数(推荐使用70)。参数值越大,锐化效果越明显。
/ignore-error/1 当处理参数中携带此参数时,针对文件过大、参数超限等导致处理失败的场景,会直接返回原图而不报错。

灰度图

功能说明

对象存储通过数据万象 imageMogr2/grayscale 接口将图片设置为灰度图。

代码示例

<?php


require dirname(__FILE__, 2) . '/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 {
    // 图片处理-灰度图 https://cloud.tencent.com/document/product/460/66519
    $imageRule = new Qcloud\Cos\ImageParamTemplate\ImageMogrTemplate();
    $imageRule->grayscale(1); // 将图片设置为灰度图 /grayscale/<value>
    $imageRule->ignoreError(); // /ignore-error/1

    $picOperations = new Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation();
    $picOperations->setIsPicInfo(1); // is_pic_info
    $picOperations->addRule($imageRule, "output.png"); // rules

    // -------------------- 1. 下载时处理 -------------------- //
//    $downloadUrl = $cosClient->getObjectUrl('examplebucket-1250000000', 'xxx.jpg'); // 获取下载链接
    $downloadUrl = 'https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/xxx.jpg'; // 私有图片处理方式同上,仅增加签名部分,并与图片处理参数以“&”连接
    $rule = $imageRule->queryString();
    echo "{$downloadUrl}?{$rule}";
//    echo "{$downloadUrl}&{$rule}"; // 携带签名的图片地址以“&”连接
    // -------------------- 1. 下载时处理 -------------------- //

    // -------------------- 2. 上传时处理 -------------------- //
    $result = $cosClient->putObject(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'object.jpg',
        'Body' => fopen('/tmp/local.jpg', 'rb'), // 本地文件
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 2. 上传时处理 -------------------- //

    // -------------------- 3. 云上数据处理 -------------------- //
    $result = $cosClient->ImageProcess(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'test.png',
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 3. 云上数据处理 -------------------- //
} catch (\Exception $e) {
    // 请求失败
    echo($e);
}

处理参数说明

操作名称:grayscale。

参数 含义
/grayscale/<value> 将图片设置为灰度图。 value 取值为0表示不改变图片。 value 取值为1表示将图片变为灰度图。
/ignore-error/1 当处理参数中携带此参数时,针对文件过大导致处理失败的场景,会直接返回原图而不报错。

图片水印

功能说明

对象存储通过数据万象 watermark 接口提供图片水印处理功能。目前水印图片必须指定为已存储于数据万象绑定(或新建)的存储桶中的图片。关于图片水印等图片基础处理的限制说明,请参见 规则与限制

代码示例

<?php


require dirname(__FILE__, 2) . '/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 {
    // 图片处理-图片水印 https://cloud.tencent.com/document/product/460/6930
    $imageWatermarkRule = new Qcloud\Cos\ImageParamTemplate\ImageWatermarkTemplate();
    $imageWatermarkRule->setImage('https://www.xxx.com/xxx.jpg'); // 水印图片地址
    $imageWatermarkRule->setGravity('SouthEast'); // 图片水印位置
    $imageWatermarkRule->setDx(10); // 水平(横轴)边距,单位为像素,缺省值为0
    $imageWatermarkRule->setDy(10); // 垂直(纵轴)边距,单位为像素,默认值为0
    $imageWatermarkRule->setBlogo(1); // 水印图适配功能,适用于水印图尺寸过大的场景(如水印墙)
    $imageWatermarkRule->setScatype(1); // 根据原图的大小,缩放调整水印图的大小
    $imageWatermarkRule->setSpcent(200); // 与 scatype 搭配使用
    $imageWatermarkRule->setDissolve(70); // 图片水印的透明度
    $imageWatermarkRule->setBatch(1); // 平铺水印功能,可将图片水印平铺至整张图片
    $imageWatermarkRule->setDegree(90); // 当 batch 值为1时生效。图片水印的旋转角度设置

    $picOperations = new Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation();
    $picOperations->setIsPicInfo(1); // is_pic_info
    $picOperations->addRule($imageWatermarkRule, "output.png"); // rules

    // -------------------- 1. 下载时处理 -------------------- //
//    $downloadUrl = $cosClient->getObjectUrl('examplebucket-1250000000', 'xxx.jpg'); // 获取下载链接
    $downloadUrl = 'https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/xxx.jpg'; // 私有图片处理方式同上,仅增加签名部分,并与图片处理参数以“&”连接
    $rule = $imageWatermarkRule->queryString();
    echo "{$downloadUrl}?{$rule}";
//    echo "{$downloadUrl}&{$rule}"; // 携带签名的图片地址以“&”连接
    // -------------------- 1. 下载时处理 -------------------- //

    // -------------------- 2. 上传时处理 -------------------- //
    $result = $cosClient->putObject(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'object.jpg',
        'Body' => fopen('/tmp/local.jpg', 'rb'), // 本地文件
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 2. 上传时处理 -------------------- //

    // -------------------- 3. 云上数据处理 -------------------- //
    $result = $cosClient->ImageProcess(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'test.png',
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 3. 云上数据处理 -------------------- //
} catch (\Exception $e) {
    // 请求失败
    echo($e);
}

处理参数说明

操作名称:watermark,数字1,表示水印类型为图片水印。

参数 含义
/image/ 水印图片地址,需要经过 URL 安全的 Base64 编码。例如,水印图片为 http://examplebucket-1250000000.cos.ap-shanghai.myqcloud.com/shuiyin_2.png,则该处编码后的字符串为 aHR0cDovL2V4YW1wbGVidWNrZXQtMTI1MDAwMDAwMC5jb3MuYXAtc2hhbmdoYWkubXlxY2xvdWQuY29tL3NodWl5aW5fMi5wbmc
/gravity/ 图片水印位置,九宫格位置(参考九宫格方位图 ),默认值为 SouthEast
/dx/ 水平(横轴)边距,单位为像素,缺省值为0
/dy/ 垂直(纵轴)边距,单位为像素,默认值为0
/blogo/ 水印图适配功能,适用于水印图尺寸过大的场景(如水印墙)。一共有两种类型: 当 blogo 设置为1时,水印图会被缩放至与原图相似大小后添加 当 blogo 设置为2时,水印图会被直接裁剪至与原图相似大小后添加
/scatype/ 根据原图的大小,缩放调整水印图的大小: 当 scatype 设置为1时,按原图的宽缩放 当 scatype 设置为2时,按原图的高缩放 当 scatype 设置为3时,按原图的整体面积缩放
/spcent/ 与 scatype 搭配使用:当 scatype 设置为1时,该有效值为[1, 1000],单位为千分比当 scatype 设置为2时,该有效值为[1, 1000],单位为千分比当 scatype 设置为3时,该有效值为[1, 250],单位为千分比。示例:http://examples-1251000004.cos.ap-shanghai.myqcloud.com/sample.jpeg?watermark/1/image/xxxxxxx/scatype/3/spcent/250
/dissolve/ 图片水印的透明度,取值为1 - 100,默认90(90%不透明度)
/batch/ 平铺水印功能,可将图片水印平铺至整张图片。值为1时,表示开启平铺水印功能
/degree/ 当 batch 值为1时生效。图片水印的旋转角度设置,取值范围为0 - 360,默认0
注意:

指定的水印图片必须同时满足如下3个条件:

  • 水印图片与源图片必须位于同一个存储桶下。
  • URL 需使用 COS 域名(不能使用 CDN 加速域名,例如 examplebucket-1250000000.file.myqcloud.com/shuiyin_2.png 不可用 ),且需保证水印图可访问(如果水印图读取权限为私有,则需要携带有效签名)。
  • URL 必须以 http:// 开始,不能省略 HTTP 头,也不能填 HTTPS 头,例如 examplebucket-1250000000.cos.ap-shanghai.myqcloud.com/shuiyin_2.pnghttps://examplebucket-1250000000.cos.ap-shanghai.myqcloud.com/shuiyin_2.png 为非法的水印 URL。

文字水印

功能说明

对象存储通过数据万象 watermark 接口提供实时文字水印处理功能。处理图片原图大小不超过32MB、宽高不超过30000像素且总像素不超过2.5亿像素,处理结果图宽高设置不超过9999像素;针对动图,原图宽 x 高 x 帧数不超过2.5亿像素。

代码示例

<?php


require dirname(__FILE__, 2) . '/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 {
    // 图片处理-文字水印 https://cloud.tencent.com/document/product/460/6951
    $textWatermarkRule = new Qcloud\Cos\ImageParamTemplate\TextWatermarkTemplate();
    $textWatermarkRule->setText('水印内容'); // 水印内容
    $textWatermarkRule->setFont('tahoma.ttf'); // 水印字体
    $textWatermarkRule->setFontsize(13); // 水印文字字体大小
    $textWatermarkRule->setFill('#3D3D3D'); // 字体颜色,缺省为灰色,需设置为十六进制 RGB 格式(例如 #FF0000)
    $textWatermarkRule->setDissolve(90); // 文字透明度,取值1 - 100 ,默认90(90%不透明度)
    $textWatermarkRule->setGravity('SouthEast'); // 文字水印位置,九宫格位置(参见九宫格方位图),默认值 SouthEast
    $textWatermarkRule->setDx(10); // 水平(横轴)边距,单位为像素,缺省值为0
    $textWatermarkRule->setDy(10); // 垂直(纵轴)边距,单位为像素,默认值为0
    $textWatermarkRule->setBatch(1); // 平铺水印功能,可将文字水印平铺至整张图片。值为1时,表示开启平铺水印功能
    $textWatermarkRule->setDegree(10); // 当 batch 值为1时生效。文字水印的旋转角度设置,取值范围为0 - 360,默认0
    $textWatermarkRule->setShadow(10); // 文字阴影效果,有效值为[0,100],默认为0,表示无阴影

    $picOperations = new Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation();
    $picOperations->setIsPicInfo(1); // is_pic_info
    $picOperations->addRule($textWatermarkRule, "output.png"); // rules

    // -------------------- 1. 下载时处理 -------------------- //
//    $downloadUrl = $cosClient->getObjectUrl('examplebucket-1250000000', 'xxx.jpg'); // 获取下载链接
    $downloadUrl = 'https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/xxx.jpg'; // 私有图片处理方式同上,仅增加签名部分,并与图片处理参数以“&”连接
    $rule = $textWatermarkRule->queryString();
    echo "{$downloadUrl}?{$rule}";
//    echo "{$downloadUrl}&{$rule}"; // 携带签名的图片地址以“&”连接
    // -------------------- 1. 下载时处理 -------------------- //

    // -------------------- 2. 上传时处理 -------------------- //
    $result = $cosClient->putObject(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'object.jpg',
        'Body' => fopen('/tmp/local.jpg', 'rb'), // 本地文件
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 2. 上传时处理 -------------------- //

    // -------------------- 3. 云上数据处理 -------------------- //
    $result = $cosClient->ImageProcess(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'test.png',
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 3. 云上数据处理 -------------------- //
} catch (\Exception $e) {
    // 请求失败
    echo($e);
}

处理参数说明

操作名称:watermark,数字2,表示水印类型为文字水印。

参数 含义
/text/ 水印内容,需要经过 URL 安全的 Base64 编码
/font/ 水印字体,需要经过 URL 安全的 Base64 编码,默认值 tahoma.ttf 。水印字体列表参考 支持字体列表
/fontsize/ 水印文字字体大小,单位为磅,缺省值13。如需按照原图片的大小比例缩放文字水印,请将文字水印转换为 PNG 图片,并参考 图片水印 文档进行配置
/fill/ 字体颜色,缺省为灰色,需设置为十六进制 RGB 格式(例如 #FF0000),详情可参考 RGB 编码表,需经过 URL 安全的 Base64 编码,默认值为 #3D3D3D
/dissolve/ 文字透明度,取值1 - 100,默认90(90%不透明度)
/gravity/ 文字水印位置,九宫格位置(参见九宫格方位图),默认值 SouthEast
/dx/ 水平(横轴)边距,单位为像素,缺省值为0
/dy/ 垂直(纵轴)边距,单位为像素,默认值为0
/batch/ 平铺水印功能,可将文字水印平铺至整张图片。值为1时,表示开启平铺水印功能
/degree/ 当 batch 值为1时生效。文字水印的旋转角度设置,取值范围为0 - 360,默认0
/shadow/ 文字阴影效果,有效值为[0,100],默认为0,表示无阴影

获取图片基本信息

功能说明

对象存储通过数据万象 imageInfo 接口查询图片基本信息,包括格式、长、宽等。

代码示例

<?php

require dirname(__FILE__, 2) . '/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 {
    $result = $cosClient->ImageInfo(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'exampleobject',
    ));
    // 请求成功
    print_r($result);
} catch (\Exception $e) {
    // 请求失败
    echo($e);
}

处理参数说明

操作名称:imageInfo。

参数 含义
Key 对象文件名,例如 folder/sample.jpg。

响应参数

参数 含义
format 图片类型。例如 png、gif 等
width 图片的宽度。单位为像素(px)
height 图片的高度。单位为像素(px)
size 图片的大小。单位为 Bytes
md5 图片的 md5 值
frame_count 图片的帧数。静态图为1,动图为对应的帧数

获取图片 EXIF

功能说明

EXIF(Exchangeable Image File)全称为可交换图像文件,可记录数码照片的拍摄参数、缩略图及其他属性信息。对象存储通过数据万象的 exif 接口获取 EXIF 信息。处理图片原图大小不超过32MB、宽高不超过30000像素且总像素不超过2.5亿像素,处理结果图宽高设置不超过9999像素;针对动图,原图宽 x 高 x 帧数不超过2.5亿像素。

代码示例

<?php

require dirname(__FILE__, 2) . '/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 {
    $result = $cosClient->ImageExif(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'exampleobject',
    ));
    // 请求成功
    print_r($result);
} catch (\Exception $e) {
    // 请求失败
    echo($e);
}

处理参数说明

操作名称:exif。

参数 含义
Key 对象文件名,例如 folder/sample.jpg。

获取图片主色调

功能说明

对象存储通过数据万象 imageAve 接口获取图片主色调信息。处理图片原图大小不超过32MB、宽高不超过30000像素且总像素不超过2.5亿像素,处理结果图宽高设置不超过9999像素;针对动图,原图宽 x 高 x 帧数不超过2.5亿像素。

代码示例

<?php

require dirname(__FILE__, 2) . '/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 {
    $result = $cosClient->ImageAve(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'exampleobject',
    ));
    // 请求成功
    print_r($result);
} catch (\Exception $e) {
    // 请求失败
    echo($e);
}

处理参数说明

操作名称:imageAve。

参数 含义
Key 对象文件名,例如 folder/sample.jpg。

去除元信息

功能说明

对象存储通过数据万象 imageMogr2 接口可去除图片元信息,包括 exif 信息。

代码示例

<?php


require dirname(__FILE__, 2) . '/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 {
    // 图片处理-去除元信息 https://cloud.tencent.com/document/product/460/36547
    $imageRule = new Qcloud\Cos\ImageParamTemplate\ImageMogrTemplate();
    $imageRule->strip(); // 去除图片元信息,包括 exif 信息  /strip
    $imageRule->ignoreError(); // /ignore-error/1

    $picOperations = new Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation();
    $picOperations->setIsPicInfo(1); // is_pic_info
    $picOperations->addRule($imageRule, "output.png"); // rules

    // -------------------- 1. 下载时处理 -------------------- //
//    $downloadUrl = $cosClient->getObjectUrl('examplebucket-1250000000', 'xxx.jpg'); // 获取下载链接
    $downloadUrl = 'https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/xxx.jpg'; // 私有图片处理方式同上,仅增加签名部分,并与图片处理参数以“&”连接
    $rule = $imageRule->queryString();
    echo "{$downloadUrl}?{$rule}";
//    echo "{$downloadUrl}&{$rule}"; // 携带签名的图片地址以“&”连接
    // -------------------- 1. 下载时处理 -------------------- //

    // -------------------- 2. 上传时处理 -------------------- //
    $result = $cosClient->putObject(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'object.jpg',
        'Body' => fopen('/tmp/local.jpg', 'rb'), // 本地文件
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 2. 上传时处理 -------------------- //

    // -------------------- 3. 云上数据处理 -------------------- //
    $result = $cosClient->ImageProcess(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'test.png',
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 3. 云上数据处理 -------------------- //
} catch (\Exception $e) {
    // 请求失败
    echo($e);
}

处理参数说明

操作名称:strip。

参数 含义
/ignore-error/1 当处理参数中携带此参数时,针对文件过大导致处理失败的场景,会直接返回原图而不报错。

快速缩略模板

功能说明

对象存储通过数据万象 imageView2 接口提供常用图片处理模板。开发者根据业务需求,只需在下载 URL 后面附加相应的参数,就可以生成相应的缩略图。处理图片原图大小不超过32MB、宽高不超过30000像素且总像素不超过2.5亿像素,处理结果图宽高设置不超过9999像素;针对动图,原图宽 x 高 x 帧数不超过2.5亿像素,动图 gif 帧数限300帧。

代码示例

<?php


require dirname(__FILE__, 2) . '/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 {
    // 图片处理-快速缩略模板 https://cloud.tencent.com/document/product/460/6929
    $imageViewRule = new Qcloud\Cos\ImageParamTemplate\ImageViewTemplate();
    $imageViewRule->setMode(1); // /<mode>
    $imageViewRule->setWidth(400); // /w/<Width>
    $imageViewRule->setHeight(600); // /h/<Height>
    $imageViewRule->setFormat('jpg'); // /format/<Format>
    $imageViewRule->setQuality(1, 85); // 1-/q/<Quality>; 2-/rq/<quality>; 3-/lq/<quality>
    $imageViewRule->ignoreError(); // /ignore-error/1

    $picOperations = new Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation();
    $picOperations->setIsPicInfo(1); // is_pic_info
    $picOperations->addRule($imageViewRule, "output.png"); // rules

    // -------------------- 1. 下载时处理 -------------------- //
//    $downloadUrl = $cosClient->getObjectUrl('examplebucket-1250000000', 'xxx.jpg'); // 获取下载链接
    $downloadUrl = 'https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/xxx.jpg'; // 私有图片处理方式同上,仅增加签名部分,并与图片处理参数以“&”连接
    $rule = $imageViewRule->queryString();
    echo "{$downloadUrl}?{$rule}";
//    echo "{$downloadUrl}&{$rule}"; // 携带签名的图片地址以“&”连接
    // -------------------- 1. 下载时处理 -------------------- //

    // -------------------- 2. 上传时处理 -------------------- //
    $result = $cosClient->putObject(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'object.jpg',
        'Body' => fopen('/tmp/local.jpg', 'rb'), // 本地文件
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 2. 上传时处理 -------------------- //

    // -------------------- 3. 云上数据处理 -------------------- //
    $result = $cosClient->ImageProcess(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'test.png',
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 3. 云上数据处理 -------------------- //
} catch (\Exception $e) {
    // 请求失败
    echo($e);
}

处理参数说明

参数 含义
/1/w/<Width>/h/<Height> 限定缩略图的宽高最小值。该操作会将图像等比缩放直至某一边达到设定最小值,之后将另一边居中裁剪至设定值。若只指定一边,则表示宽高相等的正方形 例如,原图大小为1000x500,将参数设定为?imageView2/1/w/500/h/400 后,图像会先等比缩放至800x400,之后左右各裁剪150,得到500x400大小的图像
/2/w/<Width>/h/<Height> 限定缩略图的宽高最大值。该操作会将图像等比缩放至宽高都小于设定最大值 例如,原图大小为 1000x500,将参数设定为?imageView2/2/w/500/h/400后,图像会等比缩放至500x250。如果只指定一边,则另一边自适应
/3/w/<Width>/h/<Height> 限定缩略图的宽高最小值。该操作会将图像等比缩放至宽高都大于设定最小值 例如,原图大小为 1000x500,将参数设定为?imageView2/3/w/500/h/400后,图像会等比缩放至800x400。如果只指定一边,则另一边设为相同值
/4/w/<LongEdge>/h/<ShortEdge> 限定缩略图的长边和短边的最小值分别为 LongEdge 和 ShortEdge,进行等比压缩;如果只指定一边,代表另外一边为同样的值
/5/w/<LongEdge>/h/<ShortEdge> 限定缩略图的长边和短边的最大值分别为 LongEdge 和 ShortEdge,进行等比压缩,居中裁剪;如果只指定一边,则表示宽高相等的正方形;缩放后其中一边多余的部分会被裁剪掉
/format/<Format> 目标缩略图的图片格式,Format 可为:jpg,bmp,gif,png,webp,缺省为原图格式
/q/<Quality> 图片质量,取值范围0 - 100,默认值为原图质量;取原图质量和指定质量的最小值; 后面加!(注意为英文字符),表示强制使用指定值
/rq/<quality> 图片的相对质量,取值范围0 - 100,数值以原图质量为标准。例如原图质量为80,将 rquality 设置为80后,得到处理结果图的图片质量为64(80x80%)
/lq/<quality> 图片的最低质量,取值范围0 - 100,设置结果图的质量参数最小值。 例如,原图质量为85,将 lquality 设置为80后,处理结果图的图片质量为85 例如,原图质量为60,将 lquality 设置为80后,处理结果图的图片质量会被提升至80

限制图片大小

功能说明

对象存储通过数据万象 imageMogr2/size-limit 接口可限制图片处理(例如缩放、压缩等)后的文件大小。

代码示例

<?php


require dirname(__FILE__, 2) . '/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 {
    // 图片处理-限制图片大小 https://cloud.tencent.com/document/product/460/56732
    $imageRule = new Qcloud\Cos\ImageParamTemplate\ImageMogrTemplate();
    $imageRule->sizeLimit(15, 1); // 可限制图片处理(例如缩放、压缩等)后的文件大小 /size-limit/<value>!
    $imageRule->ignoreError(); // /ignore-error/1

    $picOperations = new Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation();
    $picOperations->setIsPicInfo(1); // is_pic_info
    $picOperations->addRule($imageRule, "output.png"); // rules

    // -------------------- 1. 下载时处理 -------------------- //
//    $downloadUrl = $cosClient->getObjectUrl('examplebucket-1250000000', 'xxx.jpg'); // 获取下载链接
    $downloadUrl = 'https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/xxx.jpg'; // 私有图片处理方式同上,仅增加签名部分,并与图片处理参数以“&”连接
    $rule = $imageRule->queryString();
    echo "{$downloadUrl}?{$rule}";
//    echo "{$downloadUrl}&{$rule}"; // 携带签名的图片地址以“&”连接
    // -------------------- 1. 下载时处理 -------------------- //

    // -------------------- 2. 上传时处理 -------------------- //
    $result = $cosClient->putObject(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'object.jpg',
        'Body' => fopen('/tmp/local.jpg', 'rb'), // 本地文件
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 2. 上传时处理 -------------------- //

    // -------------------- 3. 云上数据处理 -------------------- //
    $result = $cosClient->ImageProcess(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'test.png',
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 3. 云上数据处理 -------------------- //
} catch (\Exception $e) {
    // 请求失败
    echo($e);
}

处理参数说明

操作名称:size-limit。

参数 含义
size-limit 限制图片转换后的大小,支持以兆字节(m)和千字节(k)为单位 1. 仅支持 JPG 格式的图片,可以用于限制处理后图片的大小 2. 若在尾部加上!,表示用处理后的图片大小与原图大小做比较,如果处理后的图片比原图小,则返回处理后的图片,否则返回原图。例如:examplebucket-1250000000.cos.ap-shanghai.myqcloud.com/picture.jpg?imageMogr2/size-limit/15k! 3. 建议搭配 strip 参数使用,去除图片的一些冗余信息,会有更好的效果。例如:examplebucket-1250000000.cos.ap-shanghai.myqcloud.com/picture.jpg?imageMogr2/strip/format/png/size-limit/15k!
/ignore-error/1 当处理参数中携带此参数时,针对文件过大导致处理失败的场景,会直接返回原图而不报错

管道操作符

功能说明

对象存储通过数据万象的管道操作符|实现对图片按顺序进行多种处理。用户可以通过管道操作符将多个处理参数分隔开,从而实现在一次访问中按顺序对图片进行不同处理。处理图片原图大小不超过32MB、宽高不超过30000像素且总像素不超过2.5亿像素,处理结果图宽高设置不超过9999像素;针对动图,原图宽 x 高 x 帧数不超过2.5亿像素。

代码示例

<?php


require dirname(__FILE__, 2) . '/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 {
    // 图片处理-管道操作符 https://cloud.tencent.com/document/product/460/15293
    $imageMogrRule = new Qcloud\Cos\ImageParamTemplate\ImageMogrTemplate();
    $imageMogrRule->thumbnailByScale(50);
    $imageMogrRule->rotate(50);
    $imageViewRule = new Qcloud\Cos\ImageParamTemplate\ImageViewTemplate();
    $imageViewRule->setMode(1);
    $imageViewRule->setWidth(400);
    $imageViewRule->setHeight(600);
    $imageViewRule->setQuality(1, 85);
    $ciParamChannel = new Qcloud\Cos\ImageParamTemplate\CIParamTransformation();
    $ciParamChannel->addRule($imageMogrRule); // 多种处理方式 以 “|” 连接
    $ciParamChannel->addRule($imageViewRule); // 多种处理方式 以 “|” 连接

    $picOperations = new Qcloud\Cos\ImageParamTemplate\PicOperationsTransformation();
    $picOperations->setIsPicInfo(1); // is_pic_info
    $picOperations->addRule($ciParamChannel, "output.png"); // rules

    // -------------------- 1. 下载时处理 -------------------- //
//    $downloadUrl = $cosClient->getObjectUrl('examplebucket-1250000000', 'xxx.jpg'); // 获取下载链接
    $downloadUrl = 'https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/xxx.jpg'; // 私有图片处理方式同上,仅增加签名部分,并与图片处理参数以“&”连接
    $rule = $ciParamChannel->queryString();
    echo "{$downloadUrl}?{$rule}";
//    echo "{$downloadUrl}&{$rule}"; // 携带签名的图片地址以“&”连接
    // -------------------- 1. 下载时处理 -------------------- //

    // -------------------- 2. 上传时处理 -------------------- //
    $result = $cosClient->putObject(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'object.jpg',
        'Body' => fopen('/tmp/local.jpg', 'rb'), // 本地文件
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 2. 上传时处理 -------------------- //

    // -------------------- 3. 云上数据处理 -------------------- //
    $result = $cosClient->ImageProcess(array(
        'Bucket' => 'examplebucket-1250000000', //存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.cloud.tencent.com/cos5/bucket
        'Key' => 'test.png',
        'PicOperations' => $picOperations->queryString(),
    ));
    // 请求成功
    print_r($result);
    // -------------------- 3. 云上数据处理 -------------------- //
} catch (\Exception $e) {
    // 请求失败
    echo($e);
}

处理参数说明

多个处理样式之间以管道操作符|分隔,样式按照先后顺序执行,目前最多支持10层管道。

目录