edit-icon download-icon

Image processing

Last Updated: Aug 08, 2018

Image Processing (IMG) is a massive, secure, cost-effective and highly reliable image processing service. After source images are uploaded to OSS, you can process images on any Internet device at any anytime, from anywhere through simple RESTful APIs.

For more information about IMG, see Image Processing.

Basic features

OSS offers the following IMG features:

Usage

IMG uses standard HTTP GET. You can configure IMG parameters in QueryString of a URL.

If the ACL of an image object is private read and write, only authorized users are allowed for access.

Anonymous access

You can use the following format in level-3 domains to access a processed image:

  1. http://<yourBucketName>.<yourEndpoint>/<yourObjectName>?x-oss-process=image/<yourAction>,<yourParamValue>
Parameter Description
bucket Specifies the name of a bucket.
endpoint Specifies endpoint used to access a region.
object Specifies the name of an image object.
image Specifies the reserved identifier for IMG.
action Specifies the operations on an image, such as scaling, cropping, and rotating.
param Specifies the parameter that indicates the operation on an image.

Basic operations

For example, scale an image to a width of 100 px. Adjust the height based on the ratio.

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

Customized image styles

You can use the following format in level-3 domains to access a processed image:

  1. http://<yourBucketName>.<yourEndpoint>/<yourObjectName>?x-oss-process=style/<yourStyleName>
  • style: specifies a reserved identifier of a customized image style.
  • yourStyleName: specifies the name of a custom image style. It is the name specified in the rule that is created on the OSS console.

Example:

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

Cascade operations

Cascade operations allow you to perform multiple operations on an image sequentially.

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

Example:

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

HTTPS access

IMG supports access through HTTPS. Example:

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

Authorized access

Authorized access allows you to customize an image style, HTTPS access, and cascade operations.

Use the following code to generate a signed URL for IMG:

  1. // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String bucketName = "<yourBucketName>";
  7. String objectName = "<yourObjectName>";
  8. // Create an OSSClient instance.
  9. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  10. // Configure the style for an image.
  11. String style = "image/resize,m_fixed,w_100,h_100/rotate,90";
  12. // Set the expiration time to 10 minutes.
  13. Date expiration = new Date(new Date().getTime() + 1000 * 60 * 10 );
  14. GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethod.GET);
  15. req.setExpiration(expiration);
  16. req.setProcess(style);
  17. URL signedUrl = ossClient.generatePresignedUrl(req);
  18. System.out.println(signedUrl);
  19. // Close your OSSClient.
  20. ossClient.shutdown();

Access with SDK

You can use SDK to access and process an image object.

SDK allows you to customize image styles, HTTPS access, and cascade operations. For the complete IMG code, see GitHub.

Basic operations

Use the following code to perform basic operations on an image:

  1. // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String bucketName = "<yourBucketName>";
  7. String objectName = "<yourObjectName>";
  8. // Create an OSSClient instance.
  9. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  10. // Scale the image.
  11. String style = "image/resize,m_fixed,w_100,h_100";
  12. GetObjectRequest request = new GetObjectRequest(bucketName, objectName);
  13. request.setProcess(style);
  14. ossClient.getObject(request, new File("example-resize.jpg"));
  15. // Crop the image.
  16. style = "image/crop,w_100,h_100,x_100,y_100,r_1";
  17. request = new GetObjectRequest(bucketName, objectName);
  18. request.setProcess(style);
  19. ossClient.getObject(request, new File("example-crop.jpg"));
  20. // Rotate the image.
  21. style = "image/rotate,90";
  22. request = new GetObjectRequest(bucketName, objectName);
  23. request.setProcess(style);
  24. ossClient.getObject(request, new File("example-rotate.jpg"));
  25. // Sharpen the image.
  26. style = "image/sharpen,100";
  27. request = new GetObjectRequest(bucketName, objectName);
  28. request.setProcess(style);
  29. ossClient.getObject(request, new File("example-sharpen.jpg"));
  30. // Add watermarks.
  31. style = "image/watermark,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ";
  32. request = new GetObjectRequest(bucketName, objectName);
  33. request.setProcess(style);
  34. ossClient.getObject(request, new File("example-watermark.jpg"));
  35. // Convert the image format.
  36. style = "image/format,png";
  37. request = new GetObjectRequest(bucketName, objectName);
  38. request.setProcess(style);
  39. ossClient.getObject(request, new File("example-format.png"));
  40. // Obtain image information.
  41. style = "image/info";
  42. request = new GetObjectRequest(bucketName, objectName);
  43. request.setProcess(style);
  44. ossClient.getObject(request, new File("example-info.txt"));
  45. // Close your OSSClient.
  46. ossClient.shutdown();

Customize an image style

Use the following code to customize an image style:

  1. // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String bucketName = "<yourBucketName>";
  7. String objectName = "<yourObjectName>";
  8. // Create an OSSClient instance.
  9. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  10. // Customize the image style.
  11. String style = "style/<yourCustomStyleName>";
  12. GetObjectRequest request = new GetObjectRequest(bucketName, objectName);
  13. request.setProcess(style);
  14. ossClient.getObject(request, new File("example-new.jpg"));
  15. // Close your OSSClient.
  16. ossClient.shutdown();

Cascade operations

Use the following code to perform cascade operations on an image:

  1. // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String bucketName = "<yourBucketName>";
  7. String objectName = "<yourObjectName>";
  8. // Create an OSSClient instance.
  9. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  10. // Perform cascade operations.
  11. String style = "image/resize,m_fixed,w_100,h_100/rotate,90";
  12. GetObjectRequest request = new GetObjectRequest(bucketName, objectName);
  13. request.setProcess(style);
  14. ossClient.getObject(request, new File("example-new.jpg"));
  15. // Close your OSSClient.
  16. ossClient.shutdown();

IMG tools

You can use the IMG viewer ImageStyleViever to directly view the IMG result.

Thank you! We've received your feedback.