All Products
Search
Document Center

Object Storage Service:Custom crop

Last Updated:Feb 04, 2024

If you want to resize images stored in Object Storage Service (OSS) to fit your web page or meet layout requirements, you can use the custom crop feature to crop the images into the exact size that you need.

Scenarios

  • Web page design and production: When you design the layout of a web page, you may need to crop images to a specific size to fit web page elements, such as avatars, background images, and product displays.

  • Custom image size required by social media: Different social media platforms have different size requirements for images that you want to upload, such as thumbnails, images that you can post, and story images. You need to crop source images based on the recommended size to achieve optimal display performance.

  • Mobile app development: You must crop images such as icons, startup pages, and embedded pictures in an app based on specific specifications to ensure that the images are displayed as expected on devices with different resolutions and screen sizes.

  • Image database management: To meet the sorting and archiving requirements of institutions that have a large number of image resources, such as libraries and archives, you may need to crop images to a preset size.

Parameters

Action: crop.

The following table describes the parameters.

Parameter

Description

Value range

w

The width that you want to crop.

[0, image width]

Default value: the maximum value.

h

The height that you want to crop.

[0, image height]

Default value: the maximum value.

x

The X coordinate of the area that you want to crop. The default value is the X coordinate of the upper-left corner of the image.

[0, image bound]

y

The Y coordinate of the area that you want to crop. The default value is the Y coordinate of the upper-left corner of the image.

[0, image bound]

g

The position of the area that you want to crop in a 3 x 3 grid. The image is located in a 3 x 3 grid. The grid has nine tiles.

  • nw: upper left

  • north: upper middle

  • ne: upper right

  • west: middle left

  • center: center

  • east: middle right

  • sw: lower left

  • south: lower middle

  • se: lower right

For more information about how to calculate the position of each tile, see the following table.

The following table describes how to calculate the position of each tile in a 3 x 3 grid. srcW specifies the width of the source image and srcH specifies the height of the source image.

Tile

Calculation method

nw

0, 0

north

srcW/2 - w/2, 0

ne

srcW - w, 0

west

0, srcH/2 - h/2

center

srcW/2 - w/2, srcH/2 - h/2

east

srcW - w, srcH/2 - h/2

sw

0, srcH - h

sourth

srcW/2 - w/2, srcH - h

se

srcW - w, srcH - h

Usage notes

  • If the specified starting X coordinate and Y coordinate exceed those of the source image, the BadRequest error code and the Advance cut's position is out of image. error message are returned.

  • If the width and height specified from the starting point exceed those of the source image, the source image is cropped to the boundaries.

  • You can use object URLs, OSS SDKs, or API operations to configure image processing (IMG) parameters that are used to process images. In this topic, object URLs are used. You can use object URLs to configure IMG parameters only for public-read-write images. If you want to configure IMG parameters for private images, use OSS SDKs or call API operations. For more information, see IMG implementation modes.

Examples

In the following examples, an image in a bucket named oss-console-img-demo-cn-hangzhou-3az in the China (Hangzhou) region is used as the source image. The image can be accessed over the Internet by using the following URL: