edit-icon download-icon

Image processing

Last Updated: Dec 22, 2017

OSS provides Image processing service featuring massive processing power, security, low costs, and high reliability.

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

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

Image processing basic features

Image processing provides the following features:

Usage of image processing

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 processing anonymously.

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

Access image processing 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 on OSS.

  • Image: The identifier reserved by the image processing.

  • Action: Operations by the user on images, such as scaling, cropping, and rotating.

  • Parameter: 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 processing 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

By using image Service access rules, you can implement multiple operations on an 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 processing 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 processing.

Bucket permission Object permission
Private Default
Any permission Private

The image processing URL code generated with signature is as follows:

  1. public static void GenerateIamgeUri(string bucketName)
  2. {
  3. try
  4. {
  5. var process = "image/resize,m_fixed,w_100,h_100";
  6. var req = new GeneratePresignedUriRequest(bucketName, key, SignHttpMethod.Get)
  7. {
  8. Expiration = DateTime.Now.AddSeconds(30 * 60),
  9. Process = process
  10. };
  11. // Generate a signed URI
  12. var uri = client.GeneratePresignedUri(req);
  13. Console.WriteLine("Generate Presigned Uri:{0} with process:{1} succeeded ", uri, process);
  14. }
  15. catch (Exception ex)
  16. {
  17. Console.WriteLine("Failed with error info: {0}", ex.Message);
  18. }
  19. }

Note:

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

  • GeneratePresignedUri Unit of the time-out value is seconds.

  • For complete code, see GitHub.

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 processing operations include Retrieve dominant image tones, Format conversion, Resize images, Incircle, Adaptive orientation, Brightness and Add watermarks.

  1. public static void ImageProcess(string bucketName)
  2. {
  3. try
  4. {
  5. client.PutObject(bucketName, key, imageFile);
  6. // Scaling
  7. var process = "image/resize,m_fixed,w_100,h_100";
  8. var ossObject = client.GetObject(new GetObjectRequest(bucketName, key, process));
  9. WriteToFile(dirToDownload + "/sample-resize.jpg", ossObject.Content);
  10. // Cropping
  11. process = "image/crop,w_100,h_100,x_100,y_100,r_1";
  12. ossObject = client.GetObject(new GetObjectRequest(bucketName, key, process));
  13. WriteToFile(dirToDownload + "/sample-crop.jpg", ossObject.Content);
  14. // Rotating
  15. process = "image/rotate,90";
  16. ossObject = client.GetObject(new GetObjectRequest(bucketName, key, process));
  17. WriteToFile(dirToDownload + "/sample-rotate.jpg", ossObject.Content);
  18. // Sharpening
  19. process = "image/sharpen,100";
  20. ossObject = client.GetObject(new GetObjectRequest(bucketName, key, process));
  21. WriteToFile(dirToDownload + "/sample-sharpen.jpg", ossObject.Content);
  22. // Adding watermarks
  23. process = "image/watermark,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ";
  24. ossObject = client.GetObject(new GetObjectRequest(bucketName, key, process));
  25. WriteToFile(dirToDownload + "/sample-watermark.jpg", ossObject.Content);
  26. // Converting formats
  27. process = "image/format,png";
  28. ossObject = client.GetObject(new GetObjectRequest(bucketName, key, process));
  29. WriteToFile(dirToDownload + "/sample-format.png", ossObject.Content);
  30. // Image information
  31. process = "image/info";
  32. ossObject = client.GetObject(new GetObjectRequest(bucketName, key, process));
  33. WriteToFile(dirToDownload + "/sample-info.txt", ossObject.Content);
  34. Console.WriteLine("Get Object:{0} with process:{1} succeeded ", key, process);
  35. }
  36. catch (OssException ex)
  37. {
  38. Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
  39. ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
  40. }
  41. catch (Exception ex)
  42. {
  43. Console.WriteLine("Failed with error info: {0}", ex.Message);
  44. }
  45. }

Custom styles

  1. public static void ImageProcess(string bucketName)
  2. {
  3. try
  4. {
  5. client.PutObject(bucketName, key, imageFile);
  6. // Custom styles
  7. var process = "style/oss-pic-style-w-100";
  8. var ossObject = client.GetObject(new GetObjectRequest(bucketName, key, process));
  9. WriteToFile(dirToDownload + "/sample-style.jpg", ossObject.Content);
  10. }
  11. catch (OssException ex)
  12. {
  13. Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
  14. ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
  15. }
  16. catch (Exception ex)
  17. {
  18. Console.WriteLine("Failed with error info: {0}", ex.Message);
  19. }
  20. }

Image Service access rules

  1. public static void ImageProcess(string bucketName)
  2. {
  3. try
  4. {
  5. client.PutObject(bucketName, key, imageFile);
  6. // Cascading processing
  7. var process = "image/resize,m_fixed,w_100,h_100/rotate,90";
  8. var ossObject = client.GetObject(new GetObjectRequest(bucketName, key, process));
  9. WriteToFile(dirToDownload + "/sample-style.jpg", ossObject.Content);
  10. }
  11. catch (OssException ex)
  12. {
  13. Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
  14. ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
  15. }
  16. catch (Exception ex)
  17. {
  18. Console.WriteLine("Failed with error info: {0}", ex.Message);
  19. }
  20. }
Thank you! We've received your feedback.