Image Processing (IMG)は、大規模で安全、低コストで、信頼性の高い画像処理サービスです。 ソース画像が OSS にアップロードされると、簡単な RESTful API を通して、いつでも、どこからでも、どのインターネットデバイスからでも、画像を処理できます。

IMG の詳細については、「Image Processing」をご参照ください。

IMG の完全なコードについては『GitHub』をご参照ください。

基本機能

OSS は以下の IMG 機能を提供します。

使用方法

IMG は標準の HTTP GET を使用します。 URL のクエリ文字列 に IMG パラメーターを設定します。

イメージオブジェクトの ACL が "非公開読み書き" の場合、許可されたユーザーだけがアクセスできます。

  • 匿名アクセス

    処理済みイメージにアクセスするには、第 3 レベルドメインで次の形式を使用します。

    http://<yourBucketName>.<yourEndpoint>/<yourObjectName>?x-oss-process=image/<yourAction>,<yourParamValue>
    パラメーター 説明
    bucket バケットの名前を指定します。
    endpoint リージョンへのアクセスに使用されるエンドポイントを指定します。
    object 画像オブジェクトの名前を指定します。
    image IMG の 予約済み識別子を指定します。
    action 拡大縮小、トリミング、回転など、画像に対する操作を指定します。
    param 画像に対する操作を示すパラメーターを指定します。
    • 基本操作

      たとえば、画像を 100 ピクセルの幅に拡大縮小します。 比率に基づいて高さを調整します。

      http://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,w_100
    • カスタマイズされた画像スタイル

      処理済みイメージにアクセスするには、第 3 レベルドメインで次の形式を使用します。

      http://<yourBucketName>.<yourEndpoint>/<yourObjectName>?x-oss-process=style/<yourStyleName>
      • style: カスタマイズされた画像スタイルの予約済み識別子を指定します。
      • yourStyleName: カスタム画像スタイルの名前を指定します。 OSS コンソールで作成されたルールで指定された名前です。

      例:

      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 アクセス

      IMG は HTTPS を通したアクセスをサポートしています。 例:

      https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,w_100
  • 許可されたアクセス

    許可されたアクセスにより、画像スタイル、HTTPS アクセス、およびカスケード処理をカスタマイズできます。

    次のコードを使用して IMG の署名付き URL を生成します。

    #include "oss_api.h"
    #include "aos_http_io.h"
    const char *endpoint = "<yourEndpoint>";
    const char *access_key_id = "<yourAccessKeyId>";
    const char *access_key_secret = "<yourAccessKeySecret>";
    const char *bucket_name = "<yourBucketName>";
    const char *object_name = "<yourObjectName>";
    void init_options(oss_request_options_t *options)
    {
        options->config = oss_config_create(options->pool);
        /* Use a char* string to initialize aos_string_t. */
        aos_str_set(&options->config->endpoint, endpoint);
        aos_str_set(&options->config->access_key_id, access_key_id);
        aos_str_set(&options->config->access_key_secret, access_key_secret);
        /* Determine whether the CNAME is used. The value 0 indicates that the CNAME is not used. */
        options->config->is_cname = 0;
        /* Configure network parameters, such as timeout. */
        options->ctl = aos_http_controller_create(options->pool, 0);
    }
    int main(int argc, char *argv[])
    {
        /* Call the aos_http_io_initialize method in main() to initialize global resources, such as networks and memories. */
        if (aos_http_io_initialize(NULL, 0) ! = AOSE_OK) {
            exit(1);
        }
        /* Memory pool used to manage memories, equivalent to apr_pool_t. The implementation code is included in the apr library. */
        aos_pool_t *pool;
        /* Re-create a memory pool. The second parameter is NULL, indicating that the new pool does not inherit any other memory pool. */
        aos_pool_create(&pool, NULL);
        /* Create and initialize options. This parameter includes global configuration information, such as endpoint, access_key_id, acces_key_secret, is_cname, and curl. */
        oss_request_options_t *oss_client_options;
        /* Allocate the memories in the memory pool to options. */
        oss_client_options = oss_request_options_create(pool);
        /* Initialize oss_client_options. */
        init_options(oss_client_options);
        /* Initialize parameters. */
        aos_string_t bucket;
        aos_string_t object;
        aos_table_t *params = NULL;
        aos_http_request_t *req;
        char *url_str;
        apr_time_t now;
        int64_t expire_time; 
        aos_str_set(&bucket, bucket_name);
        aos_str_set(&object, object_name);
        /* Start image processing. */
        params = aos_table_make(pool, 1);
        apr_table_set(params, OSS_PROCESS, "image/resize,m_fixed,w_100,h_100");
        req = aos_http_request_create(pool);
        req->method = HTTP_GET;
        req->query_params = params;
        /* Specify the expiration time (expire_time) in seconds. */
        now = apr_time_now();
        expire_time = now / 1000000 + 10 * 60;
        /* Generate a signed URL. */
        url_str = oss_gen_signed_url(oss_client_options, &bucket, &object, expire_time, req);
        printf("url: %s\n", url_str);
        /* Release the memory pool, that is, the memories allocated to resources during the request. */
        aos_pool_destroy(pool);
        /* Release the allocated global resources. */
        aos_http_io_deinitialize();
        return 0;
    }
  • SDK を使用したアクセス

    SDK を使用して、画像オブジェクトにアクセスして処理します。

    SDK を使用すると、画像スタイル、HTTPS アクセス、およびカスケード処理をカスタマイズできます。

    • 基本操作

      次のコードを実行して、画像に対して基本操作を実行します。

      #include "oss_api.h"
      #include "aos_http_io.h"
      const char *endpoint = "<yourEndpoint>";
      const char *access_key_id = "<yourAccessKeyId>";
      const char *access_key_secret = "<yourAccessKeySecret>";
      const char *bucket_name = "<yourBucketName>";
      const char *object_name = "<yourObjectName>";
      void init_options(oss_request_options_t *options)
      {
          options->config = oss_config_create(options->pool);
          /* Use a char* string to initialize aos_string_t. */
          aos_str_set(&options->config->endpoint, endpoint);
          aos_str_set(&options->config->access_key_id, access_key_id);
          aos_str_set(&options->config->access_key_secret, access_key_secret);
          /* Determine whether the CNAME is used. The value 0 indicates that the CNAME is not used. */
          options->config->is_cname = 0;
          /* Configure network parameters, such as timeout. */
          options->ctl = aos_http_controller_create(options->pool, 0);
      }
      int main(int argc, char *argv[])
      {
          /* Call the aos_http_io_initialize method in main() to initialize global resources, such as networks and memories. */
          if (aos_http_io_initialize(NULL, 0) ! = AOSE_OK) {
              exit(1);
          }
          /* Memory pool used to manage memories, equivalent to apr_pool_t. The implementation code is included in the apr library. */
          aos_pool_t *pool;
          /* Re-create a memory pool. The second parameter is NULL, indicating that the new pool does not inherit any other memory pool. */
          aos_pool_create(&pool, NULL);
          /* Create and initialize options. This parameter includes global configuration information, such as endpoint, access_key_id, acces_key_secret, is_cname, and curl. */
          oss_request_options_t *oss_client_options;
          /* Allocate memories in the memory pool to options. */
          oss_client_options = oss_request_options_create(pool);
          /* Initialize oss_client_options. */
          init_options(oss_client_options);
          /* Initialize parameters. */
          aos_string_t bucket;
          aos_string_t object;
          aos_string_t file;
          aos_table_t *headers = NULL;
          aos_table_t *params = NULL;
          aos_table_t *resp_headers = NULL;
          aos_status_t *resp_status = NULL;
          aos_str_set(&bucket, bucket_name);
          aos_str_set(&object, object_name);
          /* Scale the image.
          params = aos_table_make(pool, 1);
          apr_table_set(params, OSS_PROCESS, "image/resize,m_fixed,w_100,h_100");
          /* Download processed images to a local file. */
          aos_str_set(&file, "example-resize.jpg");
          resp_status = oss_get_object_to_file(oss_client_options, &bucket, &object, headers, params, &file, &resp_headers);
          if (aos_status_is_ok(resp_status)) {
              printf("get object to file succeeded\n");
          } else {
              printf("get object to file failed\n");  
          }
          /* Crop the image.
          params = aos_table_make(pool, 1);
          apr_table_set(params, OSS_PROCESS, "image/crop,w_100,h_100,x_100,y_100,r_1");
          /* Download processed images to a local file. */
          aos_str_set(&file, "example-crop.jpg");
          resp_status = oss_get_object_to_file(oss_client_options, &bucket, &object, headers, params, &file, &resp_headers);
          if (aos_status_is_ok(resp_status)) {
              printf("get object to file succeeded\n");
          } else {
              printf("get object to file failed\n");  
          }
          /* Rotate the image.
          params = aos_table_make(pool, 1);
          apr_table_set(params, OSS_PROCESS, "image/rotate,90");
          /* Download processed images to a local file. */
          aos_str_set(&file, "example-rotate.jpg");
          resp_status = oss_get_object_to_file(oss_client_options, &bucket, &object, headers, params, &file, &resp_headers);
          if (aos_status_is_ok(resp_status)) {
              printf("get object to file succeeded\n");
          } else {
              printf("get object to file failed\n");  
          }
          /* Sharpen the image.
          params = aos_table_make(pool, 1);
          apr_table_set(params, OSS_PROCESS, "image/sharpen,100");
          /* Download processed images to a local file. */
          aos_str_set(&file, "example-sharpen.jpg");
          resp_status = oss_get_object_to_file(oss_client_options, &bucket, &object, headers, params, &file, &resp_headers);
          if (aos_status_is_ok(resp_status)) {
              printf("get object to file succeeded\n");
          } else {
              printf("get object to file failed\n");  
          }
          /* Add watermarks.
          params = aos_table_make(pool, 1);
          apr_table_set(params, OSS_PROCESS, "image/watermark,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ");
          /* Download processed images to a local file. */
          aos_str_set(&file, "example-watermark.jpg");
          resp_status = oss_get_object_to_file(oss_client_options, &bucket, &object, headers, params, &file, &resp_headers);
          if (aos_status_is_ok(resp_status)) {
              printf("get object to file succeeded\n");
          } else {
              printf("get object to file failed\n");  
          }
          /* Convert the image format.
          params = aos_table_make(pool, 1);
          apr_table_set(params, OSS_PROCESS, "image/format,png");
          /* Download processed images to a local file. */
          aos_str_set(&file, "example-format.png");
          resp_status = oss_get_object_to_file(oss_client_options, &bucket, &object, headers, params, &file, &resp_headers);
          if (aos_status_is_ok(resp_status)) {
              printf("get object to file succeeded\n");
          } else {
              printf("get object to file failed\n");  
          }
          /* Obtain image information.
          params = aos_table_make(pool, 1);
          apr_table_set(params, OSS_PROCESS, "image/info");
          /* Download processed images to a local file. */
          aos_str_set(&file, "example-info.txt");
          resp_status = oss_get_object_to_file(oss_client_options, &bucket, &object, headers, params, &file, &resp_headers);
          if (aos_status_is_ok(resp_status)) {
              printf("get object to file succeeded\n");
          } else {
              printf("get object to file failed\n");  
          }
          /* Release the memory pool, that is, the memories allocated to resources during the request. */
          aos_pool_destroy(pool);
          /* Release the allocated global resources. */
          aos_http_io_deinitialize();
          return 0;
      }
    • 画像スタイルのカスタマイズ

      次のコードを実行して画像スタイルをカスタマイズします。

      #include "oss_api.h"
      #include "aos_http_io.h"
      const char *endpoint = "<yourEndpoint>";
      const char *access_key_id = "<yourAccessKeyId>";
      const char *access_key_secret = "<yourAccessKeySecret>";
      const char *bucket_name = "<yourBucketName>";
      const char *object_name = "<yourObjectName>";
      void init_options(oss_request_options_t *options)
      {
          options->config = oss_config_create(options->pool);
          /* Use a char* string to initialize aos_string_t. */
          aos_str_set(&options->config->endpoint, endpoint);
          aos_str_set(&options->config->access_key_id, access_key_id);
          aos_str_set(&options->config->access_key_secret, access_key_secret);
          /* Determine whether the CNAME is used. The value 0 indicates that the CNAME is not used. */
          options->config->is_cname = 0;
          /* Configure network parameters, such as timeout. */
          options->ctl = aos_http_controller_create(options->pool, 0);
      }
      int main(int argc, char *argv[])
      {
          /* Call the aos_http_io_initialize method in main() to initialize global resources, such as networks and memories. */
          if (aos_http_io_initialize(NULL, 0) ! = AOSE_OK) {
              exit(1);
          }
          /* Memory pool used to manage memories, equivalent to apr_pool_t. The implementation code is included in the apr library. */
          aos_pool_t *pool;
          /* Re-create a memory pool. The second parameter is NULL, indicating that the new pool does not inherit any other memory pool. */
          aos_pool_create(&pool, NULL);
          /* Create and initialize options. This parameter includes global configuration information, such as endpoint, access_key_id, acces_key_secret, is_cname, and curl. */
          oss_request_options_t *oss_client_options;
          /* Allocate the memories in the memory pool to options. */
          oss_client_options = oss_request_options_create(pool);
          /* Initialize oss_client_options. */
          init_options(oss_client_options);
          /* Initialize parameters. */
          aos_string_t bucket;
          aos_string_t object;
          aos_string_t file;
          aos_table_t *headers = NULL;
          aos_table_t *params = NULL;
          aos_table_t *resp_headers = NULL;
          aos_status_t *resp_status = NULL;
          aos_str_set(&bucket, bucket_name);
          aos_str_set(&object, object_name);
          /* Customize the image style. */
          params = aos_table_make(pool, 1);
          apr_table_set(params, OSS_PROCESS, "style/<yourCustomStyleName>");
          /* Download processed images to a local file. */
          aos_str_set(&file, "example-custom-style.jpg");
          resp_status = oss_get_object_to_file(oss_client_options, &bucket, &object, headers, params, &file, &resp_headers);
          if (aos_status_is_ok(resp_status)) {
              printf("get object to file succeeded\n");
          } else {
              printf("get object to file failed\n");  
          }
          /* Release the memory pool, that is, the memories allocated to resources during the request. */
          aos_pool_destroy(pool);
          /* Release the allocated global resources. */
          aos_http_io_deinitialize();
          return 0;
      }
    • カスケード処理

      次のコードを実行して、画像に対してカスケード処理を実行します。

      #include "oss_api.h"
      #include "aos_http_io.h"
      const char *endpoint = "<yourEndpoint>";
      const char *access_key_id = "<yourAccessKeyId>";
      const char *access_key_secret = "<yourAccessKeySecret>";
      const char *bucket_name = "<yourBucketName>";
      const char *object_name = "<yourObjectName>";
      void init_options(oss_request_options_t *options)
      {
          options->config = oss_config_create(options->pool);
          /* Use a char* string to initialize aos_string_t. */
          aos_str_set(&options->config->endpoint, endpoint);
          aos_str_set(&options->config->access_key_id, access_key_id);
          aos_str_set(&options->config->access_key_secret, access_key_secret);
          /* Determine whether the CNAME is used. The value 0 indicates that the CNAME is not used. */
          options->config->is_cname = 0;
          /* Configure network parameters, such as timeout. */
          options->ctl = aos_http_controller_create(options->pool, 0);
      }
      int main(int argc, char *argv[])
      {
          /* Call the aos_http_io_initialize method in main() to initialize global resources, such as networks and memories. */
          if (aos_http_io_initialize(NULL, 0) ! = AOSE_OK) {
              exit(1);
          }
          /* Memory pool used to manage memories, equivalent to apr_pool_t. The implementation code is included in the apr library. */
          aos_pool_t *pool;
          /* Re-create a memory pool. The second parameter is NULL, indicating that the new pool does not inherit any other memory pool. */
          aos_pool_create(&pool, NULL);
          /* Create and initialize options. This parameter includes global configuration information, such as endpoint, access_key_id, acces_key_secret, is_cname, and curl. */
          oss_request_options_t *oss_client_options;
          /* Allocate the memories in the memory pool to options. */
          oss_client_options = oss_request_options_create(pool);
          /* Initialize oss_client_options. */
          init_options(oss_client_options);
          /* Initialize parameters. */
          aos_string_t bucket;
          aos_string_t object;
          aos_string_t file;
          aos_table_t *headers = NULL;
          aos_table_t *params = NULL;
          aos_table_t *resp_headers = NULL;
          aos_status_t *resp_status = NULL;
          aos_str_set(&bucket, bucket_name);
          aos_str_set(&object, object_name);
          /* Perform cascade operations. */
          params = aos_table_make(pool, 1);
          apr_table_set(params, OSS_PROCESS, "image/resize,m_fixed,w_100,h_100");
          /* Download processed images to a local file. */
          aos_str_set(&file, "example-cascade.jpg");
          resp_status = oss_get_object_to_file(oss_client_options, &bucket, &object, headers, params, &file, &resp_headers);
          if (aos_status_is_ok(resp_status)) {
              printf("get object to file succeeded\n");
          } else {
              printf("get object to file failed\n");  
          }
          /* Release the memory pool, that is, the memories allocated to resources during the request. */
          aos_pool_destroy(pool);
          /* Release the allocated global resources. */
          aos_http_io_deinitialize();
          return 0;
      }

IMG ツール

IMG ビューア ImageStyleViever を使用して、IMG 結果を直接表示できます。