图片处理是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:

    using Aliyun.OSS;
    using Aliyun.OSS.Common;
    var endpoint = "<yourEndpoint>";
    var accessKeyId = "<yourAccessKeyId>";
    var accessKeySecret = "<yourAccessKeySecret>";
    var bucketName = "<yourBucketName>";
    var objectName = "<yourObjectName>";
    // 创建OssClient实例。
    var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
    try
    {
        var process = "image/resize,m_fixed,w_100,h_100";
        var req = new GeneratePresignedUriRequest(bucketName, objectName, SignHttpMethod.Get)
        {
            Expiration = DateTime.Now.AddHours(1),
            Process = process
        };
        // 生成带有签名的URI。
        var uri = client.GeneratePresignedUri(req);
        Console.WriteLine("Generate Presigned Uri:{0} with process:{1} succeeded ", uri, process);
    }
    catch (OssException ex)
    {
        Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
            ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Failed with error info: {0}", ex.Message);
    }
  • SDK访问

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

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

    • 基础操作

      以下代码展示了图片处理的基础操作:

      using System;
      using System.IO;
      using Aliyun.OSS;
      using Aliyun.OSS.Common;
      using Aliyun.OSS.Util;
      namespace ImageProcess
      {
          class Program
          {
              static void Main(string[] args)
              {
                  Program.ImageProcess();
                  Console.ReadKey();
              }
              public static void ImageProcess()
              {
                  var endpoint = "<yourEndpoint>";
                  var accessKeyId = "<yourAccessKeyId>";
                  var accessKeySecret = "<yourAccessKeySecret>";
                  var bucketName = "<yourBucketName>";
                  var objectName = "<yourObjectName>";
                  var localImageFilename = "<yourLocalImageFilename>";
                  var downloadDir = "<yourDownloadDir>";
                  // 创建OssClient实例。
                  var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
                  try
                  {
                      client.PutObject(bucketName, objectName, localImageFilename);
                      // 图片缩放
                      var process = "image/resize,m_fixed,w_100,h_100";
                      var ossObject = client.GetObject(new GetObjectRequest(bucketName, objectName, process));
                      WriteToFile(downloadDir + "/sample-resize.jpg", ossObject.Content);
                      // 图片裁剪
                      process = "image/crop,w_100,h_100,x_100,y_100,r_1";
                      ossObject = client.GetObject(new GetObjectRequest(bucketName, objectName, process));
                      WriteToFile(downloadDir + "/sample-crop.jpg", ossObject.Content);
                      // 图片旋转
                      process = "image/rotate,90";
                      ossObject = client.GetObject(new GetObjectRequest(bucketName, objectName, process));
                      WriteToFile(downloadDir + "/sample-rotate.jpg", ossObject.Content);
                      // 图片锐化
                      process = "image/sharpen,100";
                      ossObject = client.GetObject(new GetObjectRequest(bucketName, objectName, process));
                      WriteToFile(downloadDir + "/sample-sharpen.jpg", ossObject.Content);
                      // 图片加水印
                      process = "image/watermark,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ";
                      ossObject = client.GetObject(new GetObjectRequest(bucketName, objectName, process));
                      WriteToFile(downloadDir + "/sample-watermark.jpg", ossObject.Content);
                      // 图片格式转换
                      process = "image/format,png";
                      ossObject = client.GetObject(new GetObjectRequest(bucketName, objectName, process));
                      WriteToFile(downloadDir + "/sample-format.png", ossObject.Content);
                      // 图片信息
                      process = "image/info";
                      ossObject = client.GetObject(new GetObjectRequest(bucketName, objectName, process));
                      WriteToFile(downloadDir + "/sample-info.txt", ossObject.Content);
                      Console.WriteLine("Get Object:{0} with process:{1} succeeded ", objectName, process);
                  }
                  catch (OssException ex)
                  {
                      Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                          ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine("Failed with error info: {0}", ex.Message);
                  }
              }
              private static void WriteToFile(string filePath, Stream stream)
              {
                  using (var requestStream = stream)
                  {
                      using (var fs = File.Open(filePath, FileMode.OpenOrCreate))
                      {
                          IoUtils.WriteTo(stream, fs);
                      }
                  }
              }
          }
      }
    • 自定义样式

      以下代码用于自定义图片样式:

      using System;
      using System.IO;
      using Aliyun.OSS;
      using Aliyun.OSS.Common;
      using Aliyun.OSS.Util;
      namespace ImageProcessCustom
      {
          class Program
          {
              static void Main(string[] args)
              {
                  Program.ImageProcessCustomStyle();
                  Console.ReadKey();
              }
              public static void ImageProcessCustomStyle()
              {
                  var endpoint = "<yourEndpoint>";
                  var accessKeyId = "<yourAccessKeyId>";
                  var accessKeySecret = "<yourAccessKeySecret>";
                  var bucketName = "<yourBucketName>";
                  var objectName = "<yourObjectName>";
                  var localImageFilename = "<yourLocalImageFilename>";
                  var downloadDir = "<yourDownloadDir>";
                  // 创建OssClient实例。
                  var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
                  try
                  {
                      client.PutObject(bucketName, objectName, localImageFilename);
                      // 自定义样式:"style/<yourCustomStyleName>"。
                      var process = "style/oss-pic-style-w-100";
                      var ossObject = client.GetObject(new GetObjectRequest(bucketName, objectName, process));
                      WriteToFile(downloadDir + "/sample-style.jpg", ossObject.Content);
                      Console.WriteLine("Get Object:{0} with process:{1} succeeded ", objectName, process);
                  }
                  catch (OssException ex)
                  {
                      Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                          ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine("Failed with error info: {0}", ex.Message);
                  }
              }
              private static void WriteToFile(string filePath, Stream stream)
              {
                  using (var requestStream = stream)
                  {
                      using (var fs = File.Open(filePath, FileMode.OpenOrCreate))
                      {
                          IoUtils.WriteTo(stream, fs);
                      }
                  }
              }
          }
      }
    • 级联处理

      以下代码用于级联处理图片:

      using System;
      using System.IO;
      using Aliyun.OSS;
      using Aliyun.OSS.Common;
      using Aliyun.OSS.Util;
      namespace ImageProcessCascade
      {
          class Program
          {
              static void Main(string[] args)
              {
                  Program.ImageProcessCascade();
                  Console.ReadKey();
              }
              public static void ImageProcessCascade()
              {
                  var endpoint = "<yourEndpoint>";
                  var accessKeyId = "<yourAccessKeyId>";
                  var accessKeySecret = "<yourAccessKeySecret>";
                  var bucketName = "<yourBucketName>";
                  var objectName = "<yourObjectName>";
                  var localImageFilename = "<yourLocalImageFilename>";
                  var downloadDir = "<yourDownloadDir>";
                  // 创建OssClient实例。
                  var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
                  try
                  {
                      client.PutObject(bucketName, objectName, localImageFilename);
                      // 级联处理。
                      var process = "image/resize,m_fixed,w_100,h_100/rotate,90";
                      var ossObject = client.GetObject(new GetObjectRequest(bucketName, objectName, process));
                      WriteToFile(downloadDir + "/sample-style.jpg", ossObject.Content);
                      Console.WriteLine("Get Object:{0} with process:{1} succeeded ", objectName, process);
                  }
                  catch (OssException ex)
                  {
                      Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                          ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine("Failed with error info: {0}", ex.Message);
                  }
              }
              private static void WriteToFile(string filePath, Stream stream)
              {
                  using (var requestStream = stream)
                  {
                      using (var fs = File.Open(filePath, FileMode.OpenOrCreate))
                      {
                          IoUtils.WriteTo(stream, fs);
                      }
                  }
              }
          }
      }

图片处理工具

您可以通过可视化图片处理工具ImageStyleViever直观地看到OSS图片处理结果。