Use the auto-orient parameter to control whether OSS auto-orients images based on their orientation metadata. This topic describes the parameter and provides examples.
Parameters
Action: auto-orient
| Parameter | Valid values | Description |
|---|---|---|
[value] | 0, 1 | 0: Retain the source image orientation. 1: Auto-orient the image. |
Usage notes
If orientation parameters are not specified for the source image,
auto-orienthas no effect regardless of the value set.Most image viewers and browsers auto-orient images using EXIF data. Images you view locally may already appear correctly oriented, even before any processing is applied.
Auto-orienting an image triggers re-compression. The processed image may differ in file size from the source.
Process public images
Append the x-oss-process query parameter to the URL of a public-read or public-read-write image.
The following examples use f.jpg from the image-demo bucket in the China (Hangzhou) region:
https://image-demo.oss-cn-hangzhou.aliyuncs.com/f.jpg
Resize and retain the original orientation
Parameters:
Resize to 100 px wide:
resize,w_100Disable auto-orient:
auto-orient,0
https://image-demo.oss-cn-hangzhou.aliyuncs.com/f.jpg?x-oss-process=image/resize,w_100/auto-orient,0
Resize and auto-orient
Parameters:
Resize to 100 px wide:
resize,w_100Auto-orient:
auto-orient,1
https://image-demo.oss-cn-hangzhou.aliyuncs.com/f.jpg?x-oss-process=image/resize,w_100/auto-orient,1
Process private images
For private images, use OSS SDKs or the OSS API to include request signing.
OSS SDKs
All SDK examples below use image/resize,w_100/auto-orient,1 as the process string and load credentials from environment variables. Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running.
Java
Requires OSS SDK for Java 3.17.4 or later.
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 {
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
String region = "cn-hangzhou";
// Load credentials from environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
String bucketName = "examplebucket";
String objectName = "example.jpg";
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
String image = "image/resize,w_100/auto-orient,1";
GetObjectRequest request = new GetObjectRequest(bucketName, objectName);
request.setProcess(image);
// Save the processed image. If a file with the same name already exists, it is overwritten.
ossClient.getObject(request, new File("D:\\dest.jpg"));
} catch (OSSException oe) {
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("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}PHP
Requires OSS SDK for PHP 2.7.0 or later.
<?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;
// Load credentials from environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
$provider = new EnvironmentVariableCredentialsProvider();
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
$bucket = "examplebucket";
$object = "example.jpg";
$download_file = "D:\\dest.jpg";
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region" => "cn-hangzhou"
);
$ossClient = new OssClient($config);
$image = "image/resize,w_100/auto-orient,1";
$options = array(
OssClient::OSS_FILE_DOWNLOAD => $download_file,
OssClient::OSS_PROCESS => $image
);
// Save the processed image to the local path.
$ossClient->getObject($bucket, $object, $options);Python
Requires OSS SDK for Python 2.18.4 or later.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Load credentials from environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'
region = 'cn-hangzhou'
bucket = oss2.Bucket(auth, endpoint, 'examplebucket', region=region)
key = 'example.jpg'
new_pic = 'D:\\dest.jpg'
image = 'image/resize,w_100/auto-orient,1'
bucket.get_object_to_file(key, new_pic, process=image)Go
Requires OSS SDK for Go 3.0.2 or later.
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() {
// Load credentials from environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
HandleError(err)
}
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)
}
bucket, err := client.Bucket("examplebucket")
if err != nil {
HandleError(err)
}
sourceImageName := "example.jpg"
targetImageName := "D://dest.jpg"
image := "image/resize,w_100/auto-orient,1"
err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(image))
if err != nil {
HandleError(err)
}
}RESTful API
To call the OSS API directly, include a V4 signature in your request. For details, see (Recommended) Include a V4 signature.
Use the x-oss-process query parameter in a GetObject request:
GET /oss.jpg?x-oss-process=image/resize,w_100/auto-orient,1 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