All Products
Search
Document Center

Object Storage Service:Image processing (PHP SDK V1)

Last Updated:Mar 20, 2026

Use OSS Image Processing (IMG) to resize, rotate, watermark, and transform images stored in OSS buckets—on demand, without managing separate infrastructure.

Prerequisites

Before you begin, make sure that you have:

  • An OSS bucket with the source image uploaded

  • The OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables set

  • The OSS PHP SDK V1 installed

Set up the OssClient

All examples in this topic use the following client initialization. Set it up once, then reuse it across operations.

<?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
$provider = new EnvironmentVariableCredentialsProvider();

$config = array(
    "provider"         => $provider,
    "endpoint"         => "https://oss-cn-hangzhou.aliyuncs.com",  // Replace with your bucket's endpoint
    "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
    "region"           => "cn-hangzhou"  // Replace with your bucket's region
);

$ossClient = new OssClient($config);

$bucket = "examplebucket";               // Your bucket name
$object = "exampledir/exampleobject.jpg"; // Full object path, excluding the bucket name
This topic uses the public endpoint of the China (Hangzhou) region as an example. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For supported regions and endpoints, see Regions and endpoints. To create an OssClient instance using a custom domain name or Security Token Service (STS), see Create an OSSClient instance.

Apply image processing parameters

Pass image processing parameters in the OSS_PROCESS option when downloading an object. The processed result is saved to a local file—it does not overwrite the source object in OSS.

Apply a single parameter

The following example resizes the image to 100×100 px (fixed dimensions) and saves the result locally.

$download_file = "D:\\localpath\\example-resize.jpg";
// If only a filename is specified (no path), the file is saved to the project's working directory.

$options = array(
    OssClient::OSS_FILE_DOWNLOAD => $download_file,
    OssClient::OSS_PROCESS      => "image/resize,m_fixed,h_100,w_100"
);

$ossClient->getObject($bucket, $object, $options);
// The processed image is saved to $download_file. The source object in OSS is unchanged.

Chain multiple parameters

Separate multiple parameters with forward slashes (/). Each parameter is applied in sequence.

The following example resizes the image to 100×100 px, then rotates it 90 degrees.

$download_file = "D:\\localpath\\example-new.jpg";

$style = "image/resize,m_fixed,w_100,h_100/rotate,90";

$options = array(
    OssClient::OSS_FILE_DOWNLOAD => $download_file,
    OssClient::OSS_PROCESS      => $style
);

$ossClient->getObject($bucket, $object, $options);
// The processed image is saved to $download_file. The source object in OSS is unchanged.

For a complete list of supported parameters, see Image processing.

Apply an image style

An image style bundles multiple processing parameters into a named preset you create in the OSS console. Use a style when you need to reuse the same transformation across many images. For more information on creating styles, see Image styles.

Reference the style by name using the style/<name> format.

$download_file = "D:\\localpath\\example-new.jpg";

// Replace yourCustomStyleName with the name of the style you created in the OSS console.
$style = "style/yourCustomStyleName";

$options = array(
    OssClient::OSS_FILE_DOWNLOAD => $download_file,
    OssClient::OSS_PROCESS      => $style
);

$ossClient->getObject($bucket, $object, $options);
// The processed image is saved to $download_file.

Save a processed image to OSS

By default, IMG returns the processed image in the response without saving it. To persist the result, chain the sys/saveas operation and specify a destination object path.

The destination object name and bucket name must be Base64URL-encoded before being passed to sys/saveas.

$save_object = "example-new.jpg";  // Destination object path in the same bucket

function base64url_encode($data)
{
    return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}

// Resize and rotate, then save the result to the bucket
$style = "image/resize,m_fixed,w_100,h_100/rotate,90";

$process = $style
    . '|sys/saveas'
    . ',o_' . base64url_encode($save_object)
    . ',b_' . base64url_encode($bucket);

$result = $ossClient->processObject($bucket, $object, $process);
// Print the processing result.
print($result);

Generate a signed URL with image processing parameters

Access URLs for private objects must be signed. Image processing parameters must be included in the signature—appending them after the signed URL is not supported.

Use signUrl with the OSS_PROCESS option to embed the parameters before signing.

Resize with a signed URL

The following example generates a signed URL that resizes the image to 100×100 px. The URL is valid for 3,600 seconds and can be opened directly in a browser.

$timeout = 3600;  // URL validity in seconds

$options = array(
    OssClient::OSS_PROCESS => "image/resize,m_fixed,h_100,w_100"
);

$signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "GET", $options);
print($signedUrl);

Add a watermark with a signed URL

Watermark text must be Base64URL-encoded before being passed as a parameter. If using a watermark image instead of text, the watermark image must be stored in the same bucket as the source object.

$timeout = 3600;

function base64url_encode($data)
{
    return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}

// Set the watermark text. To use a watermark image, replace this with the object path (e.g., "panda.jpg").
$content = "Hello World";
$encoded = base64url_encode($content);

$options = array(
    OssClient::OSS_PROCESS => "image/watermark,text_" . $encoded
);

$signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "GET", $options);
print($signedUrl);

What's next