You can use indexed slice parameters to slice a source image stored in Object Storage Service (OSS) based on a specific size and retrieve the required image. Indexed slice refers to evenly dividing a source image stored in OSS into multiple partitions along a specified direction based on preset size parameters, and returning a specific partition based on the index parameter.
Parameters
Action: indexcrop
The following table describes the parameters:
Parameter | Description | Valid value |
x | The length of each image partition during horizontal slicing. Only one of the x and y parameters can be specified. | [1, image width] |
y | The length of each image partition during vertical slicing. Only one of the x and y parameters can be specified. | [1, image height] |
i | The image partition that you want to select after slicing. | [0, maximum number of partitions) The default value is 0, which specifies the first partition. If you want to return the last partition, perform indexed slicing based on the image size and the value range of the parameter. |
Usage notes
If the specified index exceeds the number of partitions, the source image is returned.
If both x and y are specified and their values are valid, the value of y takes effect.
Methods
You can set image processing parameters through file URLs, SDKs, or APIs. The file URL method is suitable for publicly accessible images, while SDKs and APIs are suitable for private images. For more information, see Image processing methods.
Slice a public-read or public-read-write image
This section provides examples on how to slice an image named example.jpg in the oss-console-img-demo-cn-hangzhou bucket in the China (Hangzhou) region. The image is hosted at the following URL:
Slice the image along the horizontal axis
Configure the following parameters to slice the image:
Slice the image by 100 pixels along the horizontal axis:
indexcrop,x_100Select the first partition:
i_0
The URL used to process the image is: https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/indexcrop,x_100,i_0

Slice the image along the vertical axis
Configure the following parameters to slice the image:
Slice the image by 100 pixels along the vertical axis:
indexcrop,y_100Select the 11th partition:
i_10
The URL used to process the image is: https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/indexcrop,y_100,i_10
This processing slices the image by 100 pixels along the y-axis and specifies to extract the 11th partition (counting from 0) using the parameter indexcrop,y_100,i_10. Because the image height is not sufficient to generate 11 partitions, meaning i=10 exceeds the actual range of partitions, the system returns the original image.

Slice a private image
You can use OSS SDKs and REST API to slice a private image.
Use OSS SDKs
The following sample code provides examples on how to slice a private image by using OSS SDK for common programming languages. For more information about how to slice a private image by using other SDKs, see SDK overview.
Java
OSS SDK for Java 3.17.4 or later is required.
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.GetObjectRequest;
import java.io.File;
public class Demo {
public static void main(String[] args) throws Throwable {
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the region of the bucket in which the object is stored. Example: cn-hangzhou.
String region = "cn-hangzhou";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the name of the bucket. Example: examplebucket.
String bucketName = "examplebucket";
// Specify the full path of the object. Do not include the bucket name in the full path.
String objectName = "example.jpg";
// Specify the full path of the local file. Example: D:\\dest.jpg. If the specified local file exists, the downloaded object overwrites the file. Otherwise, a file is created.
String pathName = "D:\\dest.jpg";
// Create an OSSClient instance.
// When the OSSClient instance is no longer in use, call the shutdown method to release resources.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Slice the image by 100 pixels along the horizontal axis and select the first partition.
String image = "image/indexcrop,x_100,i_0";
GetObjectRequest request = new GetObjectRequest(bucketName, objectName);
request.setProcess(image);
// Set the name of the processed image to dest.jpg and save it to your local computer.
// If you specify only the name of the processed image such as dest.jpg without specifying the local path, the processed image is saved to the local path of the project to which the sample program belongs.
ossClient.getObject(request, new File("D:\\dest.jpg"));
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}PHP
OSS SDK for PHP 2.7.0 or later is required.
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$provider = new EnvironmentVariableCredentialsProvider();
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the name of the bucket. Example: examplebucket.
$bucket= "examplebucket";
// Specify the full path of the object. Do not include the bucket name in the full path.
$object = "example.jpg";
// Specify the full path of the local file. Example: D:\\dest.jpg. If the specified local file exists, the downloaded object overwrites the file. Otherwise, a file is created.
// If you specify only the name of the local file such as dest.jpg without specifying the local path of the file, the processed image is saved to the local path of the project to which the sample program belongs.
$download_file = "D:\\dest.jpg";
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
// Specify the ID of the Alibaba Cloud region in which the bucket is located.
"region" => "cn-hangzhou"
);
$ossClient = new OssClient($config);
// Slice the image by 100 pixels along the horizontal axis and select the first partition.
$image = "image/indexcrop,x_100,i_0";
$options = array(
OssClient::OSS_FILE_DOWNLOAD => $download_file,
OssClient::OSS_PROCESS => $image);
// Save the processed image to your local device.
$ossClient->getObject($bucket, $object, $options); Python
OSS SDK for Python 2.18.4 or later is required.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'
# Specify the ID of the Alibaba Cloud region in which the bucket is located.
region = 'cn-hangzhou'
bucket = oss2.Bucket(auth, endpoint, 'examplebucket', region=region)
# Specify the name of the source image. If the image is not stored in the root directory of the bucket, you must include the full path of the image. Example: exampledir/src.gif.
key = 'example.jpg'
# Specify the name of the processed image.
new_pic = 'D:\\dest.jpg'
# Slice the image by 100 pixels along the horizontal axis and select the first partition.
image = 'image/indexcrop,x_100,i_0'
bucket.get_object_to_file(key, new_pic, process=image)Go
OSS SDK for Go 3.0.2 or later is required.
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func HandleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
client, err := oss.New("https://oss-cn-hangzhou.aliyuncs.com", "", "", oss.SetCredentialsProvider(&provider), oss.AuthVersion(oss.AuthV4), oss.Region("cn-hangzhou"))
if err != nil {
HandleError(err)
}
// Specify the name of the bucket in which the source image is stored. Example: examplebucket.
bucketName := "examplebucket"
bucket, err := client.Bucket(bucketName)
if err != nil {
HandleError(err)
}
// Specify the name of the source image. If the image is not stored in the root directory of the bucket, you must include the full path of the image. Example: exampledir/src.gif.
sourceImageName := "example.jpg"
// Specify the name of the processed image.
targetImageName := "D://dest.jpg"
// Slice the image by 100 pixels along the horizontal axis and select the first partition.
image := "image/indexcrop,x_100,i_0"
err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(image))
if err != nil {
HandleError(err)
}
}Use REST API
If your business requires a high level of customization, you can directly call REST API operations. To directly call an API operation, you must include the signature calculation in your code. For more information, see (Recommended) Include a V4 signature.
You can process images by adding indexed slice parameters to the GetObject operation.
GET /oss.jpg?x-oss-process=image/indexcrop,x_100,i_0 HTTP/1.1
Host: oss-example.oss-cn-hangzhou.aliyuncs.com
Date: Fri, 28 Oct 2022 06:40:10 GMT
Authorization: OSS4-HMAC-SHA256 Credential=LTAI********************/20250417/cn-hangzhou/oss/aliyun_v4_request,Signature=a7c3554c729d71929e0b84489addee6b2e8d5cb48595adfc51868c299c0c218e