All Products
Search
Document Center

Object Storage Service:Image processing (PHP SDK V1)

Last Updated:Nov 29, 2025

Image Processing (IMG) is a secure, cost-effective, and highly reliable image processing service that is provided by Object Storage Service (OSS) to help you process images. After you upload source images to OSS, you can call RESTful API operations to process the images anytime, anywhere, and on any connected devices.

Notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.

  • For more information about the supported image processing parameters, see Image processing. For the complete sample code, see GitHub.

Use image processing parameters to process images

  • Process an image with a single image processing parameter and save it as a local file

    <?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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    $provider = new EnvironmentVariableCredentialsProvider();
    // Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
    $endpoint = "yourEndpoint";
    // Set the bucket name. For example, examplebucket.
    $bucket= "examplebucket";
    // Set the full path of the object. For example, exampledir/exampleobject.jpg. The full path cannot include the bucket name.
    $object = "exampledir/exampleobject.jpg";
    // Set the full path of the local file. For example, D:\\localpath\\example-resize.jpg. If the specified local file exists, it is overwritten. If it does not exist, a new file is created.
    // If you specify only the local file name, such as example-resize.jpg, without a full path, the file is saved by default to the local path of the project where the sample program resides.
    $download_file = "D:\\localpath\\example-resize.jpg";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    // If the source image is not in the specified bucket, upload the image to the bucket.
    // $ossClient->uploadFile($bucket, $object, "D:\\localpath\\exampleobject.jpg");
    
    // Resize the image to a fixed width and height of 100 px.
    $options = array(
        OssClient::OSS_FILE_DOWNLOAD => $download_file,
        OssClient::OSS_PROCESS => "image/resize,m_fixed,h_100,w_100" );
    // Name the processed image example-resize.jpg and save it to a local path.
    $ossClient->getObject($bucket, $object, $options);
    
    // After the image is processed, delete the source image from the bucket if it is no longer needed.
    // $ossClient->deleteObject($bucket, $object);                           
  • Process an image with multiple image processing parameters and save it as a local file

    When you use multiple image processing parameters, separate them with forward slashes (/).

    <?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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
    $provider = new EnvironmentVariableCredentialsProvider();
    // Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
    $endpoint = "yourEndpoint";
    // Set the bucket name. For example, examplebucket.
    $bucket= "examplebucket";
    // Set the full path of the object. For example, exampledir/exampleobject.jpg. The full path cannot include the bucket name.
    $object = "exampledir/exampleobject.jpg";
    // Set the full path of the local file. For example, D:\\localpath\\example-new.jpg. If the specified local file exists, it is overwritten. If it does not exist, a new file is created.
    // If you specify only the local file name, such as example-new.jpg, without a full path, the file is saved by default to the local path of the project where the sample program resides.
    $download_file = "D:\\localpath\\example-new.jpg";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    // If the source image is not in the specified bucket, upload the image to the bucket.
    // $ossClient->uploadFile($bucket, $object, "D:\\localpath\\exampleobject.jpg");
    
    // Resize the image to a fixed width and height of 100 px, and then rotate it 90 degrees.
    $style = "image/resize,m_fixed,w_100,h_100/rotate,90";
    
    $options = array(
        OssClient::OSS_FILE_DOWNLOAD => $download_file,
        OssClient::OSS_PROCESS => $style);
    
    // Name the processed image example-new.jpg and save it to a local path.
    $ossClient->getObject($bucket, $object, $options);
    
    // After the image is processed, delete the source image from the bucket if it is no longer needed.
    // $ossClient->deleteObject($bucket, $object);                              

Use image styles to process images

You can create an image style in the OSS console and encapsulate multiple IMG parameters in the style. Then, you can use the style to process images. For more information, see Image styles.

The following code shows how to process an image using an image style.

<?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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "yourEndpoint";
// Set the bucket name. For example, examplebucket.
$bucket= "examplebucket";
// Set the full path of the object. For example, exampledir/exampleobject.jpg. The full path cannot include the bucket name.
$object = "exampledir/exampleobject.jpg";
// Set the full path of the local file. For example, D:\\localpath\\example-new.jpg. If the specified local file exists, it is overwritten. If it does not exist, a new file is created.
// If you specify only the local file name, such as example-new.jpg, without a full path, the file is saved by default to the local path of the project where the sample program resides.
$download_file = "D:\\localpath\\example-new.jpg";

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"=> "cn-hangzhou"
    );
    $ossClient = new OssClient($config);

// If the source image is not in the specified bucket, upload the image to the bucket.
// $ossClient->uploadFile($bucket, $object, "D:\\localpath\\exampleobject.jpg");

// Process the image using a custom style.
// Set yourCustomStyleName to the name of the image style that you created in the OSS console.
$style = "style/yourCustomStyleName";

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

// Name the processed image example-new.jpg and save it to a local path.
$ossClient->getObject($bucket, $object, $options);

// After the image is processed, delete the source image from the bucket if it is no longer needed.
// $ossClient->deleteObject($bucket, $object);                              

Image processing persistence

By default, IMG does not save processed images. You can call the ImgSaveAs operation to save the images to the bucket in which the source images are stored.

The following code shows how to perform an image processing persistence operation.

<?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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "yourEndpoint";
// Set the bucket name. For example, examplebucket.
$bucket= "examplebucket";
// Set the full path of the source object. For example, exampledir/exampleobject.jpg. The full path cannot include the bucket name.
$object = "exampledir/exampleobject.jpg";
// Set the full path of the destination object. For example, example-new.jpg.
$save_object = "example-new.jpg";

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

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"=> "cn-hangzhou"
    );
    $ossClient = new OssClient($config);
// If the source image is not in the specified bucket, upload the image to the bucket.
// $ossClient->uploadFile($bucket, $object, "D:\\localpath\\exampleobject.jpg");

// Resize the image to a fixed width and height of 100 px, and then rotate it 90 degrees.
$style = "image/resize,m_fixed,w_100,h_100/rotate,90";

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

// Name the processed image example-new.png and save it to the current bucket.
$result = $ossClient->processObject($bucket, $object, $process);
// Print the processing result.
print($result);

Generate a signed URL for a file with image processing parameters

Access URLs for private files must be signed. OSS does not support adding image processing parameters by appending them to a signed URL. To process a private file, you must add the image processing parameters to the signature. The following code provides an example:

<?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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "yourEndpoint";
// Set the bucket name. For example, examplebucket.
$bucket= "examplebucket";
// Set the full path of the object. For example, exampledir/exampleobject.jpg. The full path cannot include the bucket name.
$object = "exampledir/exampleobject.jpg";

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"=> "cn-hangzhou"
    );
    $ossClient = new OssClient($config);

// Generate a signed URL that includes image processing parameters. The URL is valid for 3,600 seconds. You can use a browser to access the URL.
$timeout = 3600;

$options = array(
    // Resize the image to a fixed width and height of 100 px.
    OssClient::OSS_PROCESS => "image/resize,m_fixed,h_100,w_100" );

$signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "GET", $options);
print("rtmp url: \n" . $signedUrl);           

To generate a signed URL for a file that contains watermark parameters, see the following example:

<?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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "yourEndpoint";
// Set the bucket name. For example, examplebucket.
$bucket= "examplebucket";
// Set the full path of the object. For example, exampledir/exampleobject.jpg. The full path cannot include the bucket name.
$object = "exampledir/exampleobject.jpg";

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"=> "cn-hangzhou"
        
    );
    $ossClient = new OssClient($config);

// Generate a signed URL that contains watermark parameters. The URL is valid for 3,600 seconds. You can use a browser to access the URL.
$timeout = 3600;

function base64url_encode($data)
{
    return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
// Set the text of the watermark, such as Hello World, or the full path of the watermark image, such as panda.jpg.
// When you add a watermark image to an image, make sure that the watermark image is stored in the same bucket as the source image.
$content = "Hello World";
$string = base64url_encode($content);

$options = array(
    // Add a watermark to the image.
    OssClient::OSS_PROCESS => "image/watermark,text_".$string
);

$signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "GET", $options);
print("rtmp url: \n" . $signedUrl);