图片处理是OSS提供的海量、安全、低成本、高可靠的图片处理服务。原始图片上传到OSS后,您可以通过简单的RESTful接口,在任何时间、任何地点、任何互联网设备上对图片进行处理。

图片处理的详细信息请参见OSS图片处理指南

图片处理功能

OSS图片处理提供以下功能:

图片处理使用

图片处理使用标准的HTTP GET请求。您可以在URL的QueryString中设置处理参数。

如果图片文件的访问权限为私有读写,必须通过授权才能进行访问。

  • 匿名访问

    您可以通过如下格式的三级域名匿名访问处理后的图片:

    http://<yourBucketName>.<yourEndpoint>/<yourObjectName>?x-oss-process=image/<yourAction>,<yourParamValue>
    参数 描述
    bucket 存储空间名称
    endpoint 存储空间所在地域的访问域名
    object 图片文件名称
    image 图片处理的保留标志符
    action 对图片做的操作,如缩放、裁剪、旋转等
    param 对图片做的操作所对应的参数
    • 基础操作

      例如,将图缩略成宽度为100,高度按比例处理:

      http://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,w_100
    • 自定义样式

      使用如下格式的三级域名匿名访问图片处理:

      http://<yourBucketName>.<yourEndpoint>/<yourObjectName>?x-oss-process=style/<yourStyleName>
      • style:自定义样式的保留标志符。
      • yourStyleName:自定义样式名称,即通过控制台自定义样式的规则名称。

      例如:

      http://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=style/oss-pic-style-w-100
    • 级联处理

      通过级联处理,可以对一张图片顺序进行多个操作,格式如下:

      http://<yourBucketName>.<yourEndpoint>/<yourObjectName>?x-oss-process=image/<yourAction1>,<yourParamValue1>/<yourAction2>,<yourParamValue2>/...

      例如:

      http://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,w_100/rotate,90
    • 支持HTTPS访问

      图片服务支持HTTPS访问,例如:

      https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,w_100
  • 授权访问

    授权访问支持自定义样式、HTTPS和级联处理。

    以下代码用于生成带签名的图片处理URL:
    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
          /* 初始化OSS账号信息 */
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        std::string Endpoint = "yourEndpoint";
        std::string BucketName = "yourBucketName";
        std::string ObjectName = "yourObjectName";
    
          /* 初始化网络等资源 */
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
     
       
        /* 生成签名URL */
        std::string Process = "image/resize,m_fixed,w_100,h_100";
        GeneratePresignedUrlRequest request(BucketName, ObjectName, Http::Get);
        request.setProcess(Process);
        auto outcome = client.GeneratePresignedUrl(request);
    
        /* 释放网络等资源 */
        ShutdownSdk();
        return 0;
    }
  • SDK访问

    对于任意权限的图片文件,都可以直接使用SDK访问和处理。

    SDK处理图片文件支持自定义样式、HTTPS和级联处理。

    • 基础操作
      以下代码展示了图片处理的基础操作:
      #include <alibabacloud/oss/OssClient.h>
      using namespace AlibabaCloud::OSS;
      
      int main(void)
      {
           /* 初始化OSS账号信息 */
          std::string AccessKeyId = "yourAccessKeyId";
          std::string AccessKeySecret = "yourAccessKeySecret";
          std::string Endpoint = "yourEndpoint";
          std::string BucketName = "yourBucketName";
          std::string ObjectName = "yourObjectName";
      
           /* 初始化网络等资源 */
          InitializeSdk();
      
          ClientConfiguration conf;
          OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
        
          /* 缩放 */
          std::string Process = "image/resize,m_fixed,w_100,h_100";
          GetObjectRequest request(BucketName, ObjectName);
          request.setProcess(Process);
          auto outcome = client.GetObject(request);
        
          /* 裁剪 */
          Process = "image/crop,w_100,h_100,x_100,y_100,r_1";
          request.setProcess(Process);
          outcome = client.GetObject(request);
        
          /* 旋转 */
          Process = "image/rotate,90";
          request.setProcess(Process);
          outcome = client.GetObject(request);
        
          /* 锐化 */
          Process = "image/sharpen,100";
          request.setProcess(Process);
          outcome = client.GetObject(request);
        
          /* 水印 */
          Process = "image/watermark,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ";
          request.setProcess(Process);
          outcome = client.GetObject(request);
        
          /* 格式转换 */
          Process = "image/format,png";
          request.setProcess(Process);
          outcome = client.GetObject(request);
        
          /* 获取图片信息 */
          request = request(BucketName, ObjectName, "image/info") ;
          outcome = client.GetObject(request);
      
          /* 释放网络等资源 */
          ShutdownSdk();
          return 0;
      }
    • 自定义样式
      以下代码用于自定义图片样式:
      #include <alibabacloud/oss/OssClient.h>
      using namespace AlibabaCloud::OSS;
      
      int main(void)
      {
           /* 初始化OSS账号信息 */
          std::string AccessKeyId = "yourAccessKeyId";
          std::string AccessKeySecret = "yourAccessKeySecret";
          std::string Endpoint = "yourEndpoint";
          std::string BucketName = "yourBucketName";
          std::string ObjectName = "yourObjectName";
      
           /* 初始化网络等资源 */
          InitializeSdk();
      
          ClientConfiguration conf;
          OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf) ;
        
          /* 自定义样式 */
          std::string Process = "style/<yourCustomStyleName";
          GetObjectRequest request(BucketName, ObjectName);
          request.setProcess(Process);
          auto outcome = client.GetObject(request);
      
          /* 释放网络等资源 */
          ShutdownSdk();
          return 0;
      }
    • 级联处理
      以下代码用于级联处理图片:
      #include <alibabacloud/oss/OssClient.h>
      using namespace AlibabaCloud::OSS;
      
      int main(void)
      {
           /* 初始化OSS账号信息 */
          std::string AccessKeyId = "yourAccessKeyId";
          std::string AccessKeySecret = "yourAccessKeySecret";
          std::string Endpoint = "yourEndpoint";
          std::string BucketName = "yourBucketName";
          std::string ObjectName = "yourObjectName";
      
           /* 初始化网络等资源 */
          InitializeSdk();
      
          ClientConfiguration conf;
          OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
        
          /* 级联处理 */
          std::string Process = "image/resize,m_fixed,w_100,h_100/rotate,90";
          GetObjectRequest request(BucketName, ObjectName);
          request.setProcess(Process);
          auto outcome = client.GetObject(request);
      
          /* 释放网络等资源 */
          ShutdownSdk();
          return 0;
      }

图片处理持久化

以下代码用于图片处理持久化:
#include <alibabacloud/oss/OssClient.h>
#include <sstream>
using namespace AlibabaCloud::OSS;

int main(void)
{
     /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
    std::string SourceObjectName = "yourSourceObjectName";
    std::string TargetObjectName = "yourTargetObjectName";

     /* 初始化网络等资源 */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
  
    /* 缩放 */
    std::string Process = "image/resize,m_fixed,w_100,h_100";
    std::stringstream ss;
    ss  << Process 
    <<"|sys/saveas"
    << ",o_" << Base64EncodeUrlSafe(TargetObjectName)
    << ",b_" << Base64EncodeUrlSafe(BucketName);
    ProcessObjectRequest request(BucketName, SourceObjectName, ss.str());
    auto outcome = client.ProcessObject(request);
  
    /* 裁剪 */
    Process = "image/crop,w_100,h_100,x_100,y_100,r_1";
    ss.str("");
    ss  << Process 
    <<"|sys/saveas"
    << ",o_" << Base64EncodeUrlSafe(TargetObjectName)
    << ",b_" << Base64EncodeUrlSafe(BucketName);
    request = request(BucketName, SourceObjectName, ss.str());
    outcome = client.ProcessObject(request);
  
    /* 旋转 */
    Process = "image/rotate,90";
    ss.str("");
    ss  << Process 
    <<"|sys/saveas"
    << ",o_" << Base64EncodeUrlSafe(TargetObjectName)
    << ",b_" << Base64EncodeUrlSafe(BucketName);
    request = request(BucketName, SourceObjectName, ss.str());
    outcome = client.ProcessObject(request);
  
    /* 锐化 */
    Process = "image/sharpen,100";
    ss.str("");
    ss  << Process 
    <<"|sys/saveas"
    << ",o_" << Base64EncodeUrlSafe(TargetObjectName)
    << ",b_" << Base64EncodeUrlSafe(BucketName);
    request = request(BucketName, SourceObjectName, ss.str());
    outcome = client.ProcessObject(request);
  
    /* 水印 */
    Process = "image/watermark,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ";
    ss.str("");
    ss  << Process 
    <<"|sys/saveas"
    << ",o_" << Base64EncodeUrlSafe(TargetObjectName)
    << ",b_" << Base64EncodeUrlSafe(BucketName);
    request = request(BucketName, SourceObjectName, ss.str());
    outcome = client.ProcessObject(request);
  
    /* 格式转换 */
    Process = "image/format,png";
    ss.str("");
    ss  << Process 
    <<"|sys/saveas"
    << ",o_" << Base64EncodeUrlSafe(TargetObjectName)
    << ",b_" << Base64EncodeUrlSafe(BucketName);
    request = request(BucketName, SourceObjectName, ss.str());
    outcome = client.ProcessObject(request);

    /* 释放网络等资源 */
    ShutdownSdk();
    return 0;
}