edit-icon download-icon

Image processing

Last Updated: Nov 03, 2017

Image processing is an image processing service provided to external users by Alibaba Cloud OSS featuring massive processing, security, low costs, and high reliability.

By uploading and storing source images in the OSS, you can process images anytime, anywhere, and on any Internet device through a simple RESTful interface.

The Image Service offers image processing interfaces. For image uploads, use the OSS upload interfaces. You can build your own image processing service based on OSS Image Service.

Image Service basic features

Image processing provides the following features:

Usage of Image Service

Image processing uses the standard HTTP GET request for access. All processing parameters are encoded in the QueyString in the URL.

Anonymous access

If the access permission of the image object is public read, as shown in the following table, you can access Image Service anonymously.

Bucket permission Object permission
Public-read or
Public-read-write
Default
Any permission Public-read or
Public-read-write

Access Image Service anonymously using the third-level domain in the following format:

  1. http://bucket.<endpoint>/object?x-oss-process=image/action,parame_value
  • bucket: Name of the user’s bucket
  • endpoint: The access domain name for the data center of the user’s bucket
  • object: The image object uploaded by the user to OSS
  • image: The identifier reserved by the Image Service
  • action: Operations by the user on images, such as scaling, cropping, and rotating
  • parame: Parameters corresponding to the operations by the user on the image

For example,

  1. http://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,w_100

Custom style. Access Image Service anonymously using the third-level domain in the following format:

  1. http://bucket.<endpoint>/object?x-oss-process=x-oss-process=style/name
  • style: The identifier reserved by the stem for the user’s custom style
  • name: Name of the custom style, that is, the rule name of the console-defined style

For example,

  1. http://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=style/oss-pic-style-w-100

Through cascading processing, you can implement multiple operations on the image. The format is as follows:

  1. http://bucket.<endpoint>/object?x-oss-process=image/action,parame_value/action,parame_value/...

For example,

  1. http://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,w_100/rotate,90

Image Service supports HTTPS access. For example,

  1. https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,w_100

Authorized access

For objects with private permissions, as shown in the following table, you must be authorized to access Image Service.

Bucket permission Object permission
Private Default
Any permission Private

The Image Service URL code generated with signature is as follows:

  1. aos_pool_t *p;
  2. oss_request_options_t *options;
  3. aos_http_request_t *req;
  4. char *url_str;
  5. aos_table_t *params = NULL;
  6. aos_string_t bucket;
  7. aos_string_t object;
  8. apr_time_t now;
  9. int64_t expire_time;
  10. aos_pool_create(&p, NULL);
  11. /* Create and initialize options */
  12. options = oss_request_options_create(p);
  13. init_sample_request_options(options, AOS_FALSE);
  14. /* Initialize parameters */
  15. aos_str_set(&bucket, "<Name of your bucket>");
  16. aos_str_set(&object, "<Name of your object>");
  17. /* Image processing */
  18. params = aos_table_make(p, 1);
  19. apr_table_set(params, OSS_PROCESS, "image/resize,m_fixed,w_100,h_100");
  20. req = aos_http_request_create(p);
  21. req->method = HTTP_GET;
  22. req->query_params = params;
  23. /* Expiration time. Unit: seconds */
  24. now = apr_time_now();
  25. expire_time = now / 1000000 + 10 * 60;
  26. /* Generate a signature URL */
  27. url_str = oss_gen_signed_url(options, &bucket, &object, expire_time, req);
  28. printf("url:%s\n", url_str);
  29. aos_pool_destroy(p);

Note:

  • Authorized access supports custom styles, HTTPS, and cascading processing.

  • The unit of oss_gen_signed_url expiration time is seconds.

SDK access

You can directly use SDK to access and process image objects with any permissions.

Note:

  • For complete code, see GitHub.
  • Image object processing by using SDK supports custom styles, HTTPS, and cascading processing.

Basic operations

Basic Image Service operations include Retrieve dominant image tones, Format conversion, Resize images, Incircle, Adaptive orientation, Brightness and Add watermarks.

  1. aos_pool_t *p = NULL;
  2. aos_string_t bucket;
  3. aos_string_t object;
  4. oss_request_options_t *options = NULL;
  5. aos_table_t *headers = NULL;
  6. aos_table_t *params = NULL;
  7. aos_table_t *resp_headers = NULL;
  8. aos_status_t *s = NULL;
  9. aos_string_t filename;
  10. char *style = NULL;
  11. aos_pool_create(&p, NULL);
  12. options = oss_request_options_create(p);
  13. init_sample_request_options(options, AOS_FALSE);
  14. aos_str_set(&bucket, "<Name of your bucket>");
  15. aos_str_set(&object, "<Name of your object>");
  16. aos_str_set(&filename, "<Path for storing processed images>");
  17. params = aos_table_make(p, 1);
  18. style = "image/resize,m_fixed,w_100,h_100";
  19. apr_table_set(params, OSS_PROCESS, "image/resize,m_fixed,w_100,h_100");
  20. /* Download processed images to a local file */
  21. s = oss_get_object_to_file(options, &bucket, &object, headers,
  22. params, &filename, &resp_headers);
  23. if (aos_status_is_ok(s)) {
  24. printf("get object to file succeeded\n");
  25. } else {
  26. printf("get object to file failed\n");
  27. }
  28. aos_pool_destroy(p);

Custom styles

  1. aos_pool_t *p = NULL;
  2. aos_string_t bucket;
  3. aos_string_t object;
  4. oss_request_options_t *options = NULL;
  5. aos_table_t *headers = NULL;
  6. aos_table_t *params = NULL;
  7. aos_table_t *resp_headers = NULL;
  8. aos_status_t *s = NULL;
  9. aos_string_t filename;
  10. char *style = NULL;
  11. aos_pool_create(&p, NULL);
  12. options = oss_request_options_create(p);
  13. init_sample_request_options(options, AOS_FALSE);
  14. aos_str_set(&bucket, "<Name of your bucket>");
  15. aos_str_set(&object, "<Name of your object>");
  16. aos_str_set(&filename, "<Path for storing processed images>");
  17. /* Customize the style */
  18. style = "style/oss-pic-style-w-100";
  19. params = aos_table_make(p, 1);
  20. apr_table_set(params, OSS_PROCESS, "image/resize,m_fixed,w_100,h_100");
  21. /* Download processed images to a local file */
  22. s = oss_get_object_to_file(options, &bucket, &object, headers,
  23. params, &filename, &resp_headers);
  24. if (aos_status_is_ok(s)) {
  25. printf("get object to file succeeded\n");
  26. } else {
  27. printf("get object to file failed\n");
  28. }
  29. aos_pool_destroy(p);

Cascading processing

  1. aos_pool_t *p = NULL;
  2. aos_string_t bucket;
  3. aos_string_t object;
  4. oss_request_options_t *options = NULL;
  5. aos_table_t *headers = NULL;
  6. aos_table_t *params = NULL;
  7. aos_table_t *resp_headers = NULL;
  8. aos_status_t *s = NULL;
  9. aos_string_t filename;
  10. char *style = NULL;
  11. aos_pool_create(&p, NULL);
  12. options = oss_request_options_create(p);
  13. init_sample_request_options(options, AOS_FALSE);
  14. aos_str_set(&bucket, "<Name of your bucket>");
  15. aos_str_set(&object, "<Name of your object>");
  16. aos_str_set(&filename, "<Path for storing processed images>");
  17. /* Cascading processing */
  18. style = "image/resize,m_fixed,w_100,h_100/rotate,90";
  19. params = aos_table_make(p, 1);
  20. apr_table_set(params, OSS_PROCESS, "image/resize,m_fixed,w_100,h_100");
  21. /* Download processed images to a local file */
  22. s = oss_get_object_to_file(options, &bucket, &object, headers,
  23. params, &filename, &resp_headers);
  24. if (aos_status_is_ok(s)) {
  25. printf("get object to file succeeded\n");
  26. } else {
  27. printf("get object to file failed\n");
  28. }
  29. aos_pool_destroy(p);
Thank you! We've received your feedback.