Community Blog Easy Image Processing With OSS - Friday Blog, Week 53

Easy Image Processing With OSS - Friday Blog, Week 53

Learn how to process images in OSS directly, just by adding URL parameters!

By: Jeremy Pedersen

Advanced OSS: Image Processing

Alibaba Cloud users with some experience on AWS often compare Alibaba Cloud's Object Storage Service (OSS) to Amazon's S3.

This is a fair comparison, as both are designed to fulfill similar roles, and they offer similar features, such as:

  • Object versioning
  • Multi-part upload for large objects
  • Read-after-write consistency
  • Multi-zone redundancy
  • Automatic scaling
  • The ability to make each object accessible from the Internet via a unique object URL

...and a lot more.

However they are not identical. There are a few unique OSS features which aren't present in S3, including:

  • The ability to add content to the end of an existing object via the "append upload" feature
  • The ability to directly inject RTMP video streams into OSS
  • The ability to automatically unzip uploaded .ZIP files
  • The ability to perform image processing tasks such as resizing, watermarking, or rotation (the topic of today's blog)

Of course, some of these things can be implemented on AWS by pairing S3 with other services (like Lambda). The advantage of OSS is that these are built-ins that work without any external dependencies!

In particular, the image processing built-ins are a powerful way to work with image content.

In today's blog post we'll dive into this feature and learn how it can be used to perform a variety of common image processing tasks. Let's go!

Step 1: Setting Up A Bucket And Uploading Some Images

To test out the image processing features built into OSS, we first need to set up an OSS bucket (of course!) and add some images.

Here, I create a bucket in Singapore:


Note that the image processing feature won't be affected by your logging, versioning, or storage redundancy (LRS/ZRS) settings. In my case, I set up my bucket to use LRS (local redundant storage), and I have turned off real-time log search and object versioning. Feel free to turn those on if you like. Again, it won't affect the image processing functionality.

Next we need to find some images to work with. To avoid copyright issues, I'll use a few of my own images (feel free to reuse them for your own OSS tests!):



Next, I upload these images to my OSS bucket:





Note: You should make sure your bucket's ACL policy is set to "Public Read" so we can access the images from the Internet without a signing key. You can do this from "Access Control":


Step 2: Understanding URL parameters

OSS image processing is done by adding URL parameters to the end of the OSS URL which points to your image.

For example, let's assume you start with this image (a photo of a dog, as used in the OSS documentation):


To use OSS's built-in image processing features, we would need to add the URL parameter ?x-oss-process= to the end of the URL. We then specify one or more operations to perform, such as resize. Here, we resize the image's width to 500 pixels:


Let's break down the URL parameters at the end of the URL (?x-oss-process=image/resize,w_500,limit_0):

  • ?x-oss-process= tells OSS that we want to perform image processing
  • image/ indicates that we will be using one of OSS's built-in image processing primitives (in this case, resize). It is also possible to combine multiple image processing steps into a "style". We'll talk about that later.
  • resize is the name of the operation we want to perform (there are many others, such as blur, watermark, and crop)
  • The other values following resize, are parameters for the resize function. All these parameters follow the format <parameter name>_<value>. So w_500 means "resize the image to a width of 500 pixels". The parameter limit_0 tells OSS that we should scale the image up, if its new width or height is larger than the original image file.

There is also an m parameter that sets the "mode". This tells OSS's image processing engine what to do in cases where the new image has a different width-to-height ratio than the original. Should the image be cropped? Padded? Scaled and then cropped? Let's play with this a little bit, using one of our sample images.


The image of the bridge (see above) which I uploaded to OSS has an original size of 2014x1120. It's a PNG file.

Let's try forcing the width down to 200 pixels. To do this, I append ?x-oss-process=image/resize,w_200,limit_0 to the object URL, which gives me:


And a corresponding "tiny" image:


I can add an h_ parameter to scale the height of my image as well. Let's see what happens when I set both width and height to "500":



If you look closely, you'll realize that OSS seems to be ignoring the h_500 parameter.

Why is this? OSS has several different "modes" which can be used to resize images. The default mode is "lfit" (all the modes are documented here). They are a bit tricky to explain, but I'll do my best. The default mode is "lfit".

Very roughly explained, the modes are:

  • "lfit": Preserve the image's width-to-height ratio, giving priority to whichever scaling parameter (w_ or h_) produces the smallest final image


  • "mfit": Preserve the image's width-to-height ratio, and scale based on whichever scaling parameter (w_ or h_) results in the biggest final image


  • "fill": Do not preserve the width-to-height ratio. Instead, scale the image as-needed, and crop to fit the w_ and h_ parameters:

    • Resizing takes place before cropping
    • During resizing, width-to-height ratio is preserved, and the image is scaled along whichever side (width or height) produces the smallest final image
    • The final cropped image is centered at the middle of the resized image


  • "pad": Resize the image to the maximum size that will fit within the w_ and h_ given while preserving its width-to-height ratio, then fill the extra space with padding:


Optionally, the padding color can be controlled with a color parameter that accepts hexadecimal color values, so adding color_FF0000 would pad the image with red.

  • "fixed": Do not preserve the width-to-height ratio. Deform the image to match whatever w_ and h_ parameters are supplied.



OSS also lets us rotate images, by specifying a number of degrees (0,360) by which to rotate the image in the clockwise direction. Let's rotate our bridge by 30 degrees:



Note that the space around the image will be padded (as above).

Other Operations

There's a long list of other operations OSS can handle for us, including:

  • Cropping
  • Adding watermarks
  • File format conversion
  • Adjusting image quality
  • Making indexed cuts
  • Custom cropping (including circular or "incircle" crops)
  • Blur / sharpen / adjust contrast

See the OSS documentation for a complete list! It's far too much to cover in a single blog post.

Step 3: Creating Templates

What happens if you need to apply multiple operations to an image? One way to handle this would be to append more operations to the end of our URL parameters, like this:



This works, but "stacking" operations in this way quickly becomes complex (and results in some very messy looking URLs).

A better way to apply multiple operations at once is to group them into a "style". You can create styles from your bucket's "Data Processing" settings:


Let's try making one ourselves!

Creating Our Own "Image Enhancement & Branding" Style

Let's create a style that applies the following operations:

  • Resize images to 1000x500, padding as necessary
  • Enhance contrast
  • Add a watermark in the lower right-hand corner that says "Alibaba Cloud" (in yellow)

First, open up the "Create Rule" dialog:


Then, switch to "Advanced Edit" and paste in the following:



Click "OK" to save the new style, which will now appear in the list of image processing rules:


Now we can easily use the new style just by adding ?x-oss-process=style/branding to the end of image URLs, like this:




Next Steps

Be sure to check out the full list of OSS image processing features. I highly recommend playing around with the image processing features on your own, to get a feel for what's possible!

I've Got A Question!

Great! Reach out to me at jierui.pjr@alibabacloud.com and I'll do my best to answer in a future Friday Q&A blog.

You can also follow the Alibaba Cloud Academy LinkedIn Page. We'll re-post these blogs there each Friday.

Not a LinkedIn person? We're also on Twitter and YouTube.

0 0 0
Share on


71 posts | 152 followers

You may also like



71 posts | 152 followers

Related Products