edit-icon download-icon

Image processing

Last Updated: Dec 22, 2017

Image processing is an image processing service provided to external users by OSS featuring massive processing, 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 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
  • 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 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 process, 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 Service also 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. <?php
  2. require_once __DIR__ . '/vendor/autoload.php';
  3. use OSS\OssClient;
  4. $endpoint = "<endpoint, such as http://oss-cn-hangzhou.aliyuncs.com>";
  5. $accessKeyId = "<access_key_id>";
  6. $accessKeySecret = "<access_key_secret>";
  7. $bucket = "<bucket_name>";
  8. $object = "example.jpg";
  9. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  10. /**
  11. * Generate a signed URL that can open in the browser directly. The validity period of the URL is 3,600 seconds.
  12. */
  13. $timeout = 3600;
  14. $options = array(
  15. OssClient::OSS_PROCESS => "image/resize,m_lfit,h_100,w_100" );
  16. $signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "GET", $options);
  17. Common::println("rtmp url: \n" . $signedUrl);

Note:

  • Authorized access supports custom styles, HTTPS, and cascading processing.
  • Unit of the signedUrl time-out value 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. <?php
  2. require_once __DIR__ . '/vendor/autoload.php';
  3. use OSS\OssClient;
  4. $endpoint = "<endpoint, such as http://oss-cn-hangzhou.aliyuncs.com>";
  5. $accessKeyId = "<access_key_id>";
  6. $accessKeySecret = "<access_key_secret>";
  7. $bucket = "<bucket_name>";
  8. $object = "example.jpg";
  9. $download_file = "download.jpg";
  10. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  11. // First upload the local example.jpg to the specified $bucket and name it $object
  12. $ossClient->uploadFile($bucket, $object, "example.jpg");
  13. // Scaling
  14. $options = array(
  15. OssClient::OSS_FILE_DOWNLOAD => $download_file,
  16. OssClient::OSS_PROCESS => "image/resize,m_fixed,h_100,w_100" );
  17. $ossClient->getObject($bucket, $object, $options);
  18. // Cropping
  19. $options = array(
  20. OssClient::OSS_FILE_DOWNLOAD => $download_file,
  21. OssClient::OSS_PROCESS => "image/crop,w_100,h_100,x_100,y_100,r_1" );
  22. $ossClient->getObject($bucket, $object, $options);
  23. // Rotating
  24. $options = array(
  25. OssClient::OSS_FILE_DOWNLOAD => $download_file,
  26. OssClient::OSS_PROCESS => "image/rotate,90" );
  27. $ossClient->getObject($bucket, $object, $options);
  28. // Sharpening
  29. $options = array(
  30. OssClient::OSS_FILE_DOWNLOAD => $download_file,
  31. OssClient::OSS_PROCESS => "image/sharpen,100" );
  32. $ossClient->getObject($bucket, $object, $options);
  33. // Image watermarks
  34. $options = array(
  35. OssClient::OSS_FILE_DOWNLOAD => $download_file,
  36. OssClient::OSS_PROCESS => "image/watermark,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ" );
  37. $ossClient->getObject($bucket, $object, $options);
  38. // Converting formats
  39. $options = array(
  40. OssClient::OSS_FILE_DOWNLOAD => $download_file,
  41. OssClient::OSS_PROCESS => "image/format,png" );
  42. $ossClient->getObject($bucket, $object, $options);
  43. // Get image information
  44. $options = array(
  45. OssClient::OSS_FILE_DOWNLOAD => $download_file,
  46. OssClient::OSS_PROCESS => "image/info" );
  47. $ossClient->getObject($bucket, $object, $options);
  48. // Delete the uploaded $object
  49. $ossClient->deleteObject($bucket, $object);

Custom styles

  1. <?php
  2. require_once __DIR__ . '/vendor/autoload.php';
  3. use OSS\OssClient;
  4. $endpoint = "<endpoint, such as http://oss-cn-hangzhou.aliyuncs.com>";
  5. $accessKeyId = "<access_key_id>";
  6. $accessKeySecret = "<access_key_secret>";
  7. $bucket = "<bucket_name>";
  8. $object = "example.jpg";
  9. $download_file = "download.jpg";
  10. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  11. // First upload the local example.jpg to the specified $bucket and name it $object
  12. $ossClient->uploadFile($bucket, $object, "example.jpg");
  13. $style = "style/oss-pic-style-w-300";
  14. $options = array(
  15. OssClient::OSS_FILE_DOWNLOAD => $download_file,
  16. OssClient::OSS_PROCESS => $style);
  17. $ossClient->getObject($bucket, $object, $options);
  18. // Delete the uploaded $object
  19. $ossClient->deleteObject($bucket, $object);

Cascading processing

  1. <?php
  2. require_once __DIR__ . '/vendor/autoload.php';
  3. use OSS\OssClient;
  4. $endpoint = "<endpoint, such as http://oss-cn-hangzhou.aliyuncs.com>";
  5. $accessKeyId = "<access_key_id>";
  6. $accessKeySecret = "<access_key_secret>";
  7. $bucket = "<bucket_name>";
  8. $object = "example.jpg";
  9. $download_file = "download.jpg";
  10. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
  11. // First upload the local example.jpg to the specified $bucket and name it $object
  12. $ossClient->uploadFile($bucket, $object, "example.jpg");
  13. $style = "image/resize,m_fixed,w_100,h_100/rotate,90";
  14. $options = array(
  15. OssClient::OSS_FILE_DOWNLOAD => $download_file,
  16. OssClient::OSS_PROCESS => $style);
  17. $ossClient->getObject($bucket, $object, $options);
  18. // Delete the uploaded $object
  19. $ossClient->deleteObject($bucket, $object);

Image processing tools

  • Visualized image processing tool ImageStyleViewver can help you see the OSS image processing result intuitively.

  • Demonstration Pages of OSS Image Service features and usage.

Thank you! We've received your feedback.