Image Processing (IMG) provided by OSS is a secure, cost-effective, and highly reliable image processing service that processes large amounts of data. After you upload source images to OSS, you can call RESTful APIs to process the images anytime, anywhere, and on any Internet device.

For more information about the IMG parameters, see Parameters.

For the complete IMG code, visit GitHub.

Use the IMG parameters to process images

  • Use a single IMG parameter to process an image and save the image as the local image
    <? 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\OssClient;
    
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
    $accessKeyId = "<yourAccessKeyId>";
    $accessKeySecret = "<yourAccessKeySecret>";
    
    // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    $bucket = "<yourBucketName>";
    $object = "<yourObjectName>";
    // Specify the name of the processed image.
    $download_file = "<LocalFileName>";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    
    // If the image does not exist in the required bucket, you must upload the image to the required bucket.
    // $ossClient->uploadFile($bucket, $object, "<yourLocalFile>");
    
    // After you resize the image to a height and width of 100 pixels, save the image to your local computer.
    $options = array(
        OssClient::OSS_FILE_DOWNLOAD => $download_file,
        OssClient::OSS_PROCESS => "image/resize,m_fixed,h_100,w_100" );
    $ossClient->getObject($bucket, $object, $options);
    
    // After the IMG is complete, you can delete the source image if you no longer use the image in the bucket.
    // $ossClient->deleteObject($bucket, $object);
                                
  • Use multiple IMG parameters to process an image and save the image as the local image
    When you use multiple IMG parameters to process the image, separate these parameters 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\OssClient;
    
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
    $accessKeyId = "<yourAccessKeyId>";
    $accessKeySecret = "<yourAccessKeySecret>";
    // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    $bucket = "<yourBucketName>";
    $object = "<yourObjectName>";
    // Specify the name of the processed image.
    $download_file = "<LocalFileName>";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    // If the image does not exist in the required bucket, you must upload the image to the required bucket.
    // $ossClient->uploadFile($bucket, $object, "<yourLocalFile>");
    
    // After you resize the image to a height and width of 100 pixels, rotate the image 90°, and save the image to your local computer.
    $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);
    
    // After the IMG is complete, you can delete the source image if you no longer use the image in the bucket.
    // $ossClient->deleteObject($bucket, $object);
                                

Use image style to process images

You can encapsulate multiple IMG parameters in a style, and use the style to process multiple images at a time. For more information, see Image style. The following code provides an example on how to use image style to process an image:
<? 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\OssClient;
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
$bucket = "<yourBucketName>";
$object = "<yourObjectName>";
// Specify the name of the processed image.
$download_file = "<LocalFileName>";

$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

// If the image does not exist in the required bucket, you must upload the image to the required bucket.
// $ossClient->uploadFile($bucket, $object, "<yourLocalFile>");

// Specify the image style.
$style = "style/<yourCustomStyleName>";
// Save the image to your local computer.
$options = array(
    OssClient::OSS_FILE_DOWNLOAD => $download_file,
    OssClient::OSS_PROCESS => $style);
$ossClient->getObject($bucket, $object, $options);
// After the IMG is complete, you can delete the source image if you no longer use the image in the bucket.
// $ossClient->deleteObject($bucket, $object);
                            

IMG persistence

You can call the ImgSaveAs operation to save the images to the bucket where the source images are stored. The following code provides an example on how to save an processed image:
<? 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\OssClient;

// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
$bucket= "<yourBucketName>";
$object = "<yourObjectName>";
// Specify the name of the processed image.
$save_object = "<saveObjectName>";

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

$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
// If the image does not exist in the required bucket, you must upload the image to the required bucket.
// $ossClient->uploadFile($bucket, $object, "<yourLocalFile>");

// After you resize the image to a height and width of 100 pixels, rotate the image 90°.
$style = "image/resize,m_fixed,w_100,h_100/rotate,90";

// Save the processed image to the current bucket.
$process = $style.
           '|sys/saveas'.
           ',o_'.base64url_encode($save_object).
           ',b_'.base64url_encode($bucket);
$result = $ossClient->processObject($bucket, $object, $process);

// Display the processed result.
print($result);

Generate a signed object URL that includes IMG parameters

URLs of private objects must be signed. OSS does not allow you to add IMG parameters to a signed URL. If you want to process private objects, add IMG parameters to the signature. The following code provides an example on how to add IMG parameters to the signature:
<? 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\OssClient;

// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
$bucket= "<yourBucketName>";
$object = "<yourObjectName>";

$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

// Generate a signed URL that contains the IMG parameters. The validity period of the URL is 3,600 seconds. The signed URL can be directly accessed by using the browser.
$timeout = 3600;
$options = array(
    OssClient::OSS_PROCESS => "image/resize,m_lfit,h_100,w_100" );
$signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "GET", $options);

print("rtmp url: \n" . $signedUrl);