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

Note For more information about IMG parameters, see Parameters. For the complete IMG code, visit GitHub.

Use IMG parameters to process images

  • The following code provides an example on how to use a single IMG parameter to process an image and save the image to your local computer:
    <?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 access OSS because the account has permissions on all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
    $endpoint = "yourEndpoint";
    // Specify the name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    // Specify the full path of the object. Example: exampledir/exampleobject.jpg. The full path of the object cannot contain the bucket name. 
    $object = "exampledir/exampleobject.jpg";
    // Specify the full local path to which you want to save the image. Example: D:\\localpath\\example-resize.jpg. If the specified local file exists, it is overwritten by the processed image. Otherwise, the local file is created. 
    // By default, if you set this parameter to the name of a local file such as example-resize.jpg without specifying the local path of the file, the processed object is saved to the local path of the project to which the sample program belongs. 
    $download_file = "D:\\localpath\\example-resize.jpg";
    
    $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, "D:\\localpath\\exampleobject.jpg");
    
    // Resize the image to the height and width of 100 pixels. 
    $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 the image to your local computer. 
    $ossClient->getObject($bucket, $object, $options);
    
    // After the image is processed, you can delete the source image from the bucket if you no longer need the image. 
    // $ossClient->deleteObject($bucket, $object);                           
  • Use multiple IMG parameters to process an image and save the image to your local computer
    The following code provides an example on how to use multiple IMG parameters to process an 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 access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
    $endpoint = "yourEndpoint";
    // Specify the name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    // Specify the full path of the object. Example: exampledir/exampleobject.jpg. The full path of the object cannot contain the bucket name. 
    $object = "exampledir/exampleobject.jpg";
    // Specify the local path to which you want to save the image. Example: D:\\localpath\\example-new.jpg. If the specified local file exists, it is overwritten by the processed image. Otherwise, the local file is created. 
    // By default, if you set this parameter to the name of a local file such as example-new.jpg without specifying the local path of the file, the processed object is saved to the local path of the project to which the sample program belongs. 
    $download_file = "D:\\localpath\\example-new.jpg";
    
    $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, "D:\\localpath\\exampleobject.jpg");
    
    // After you resize the image to the height and width of 100 pixels, rotate the image 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 the image to your local computer. 
    $ossClient->getObject($bucket, $object, $options);
    
    // After the image is processed, you can delete the source image if you no longer use the image from the bucket. 
    // $ossClient->deleteObject($bucket, $object);                              

Use image style to process images

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

The following code provides an example on how to use an 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 access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
$accessKeyId = "yourAccessKeyId";
$accessKeySecret = "yourAccessKeySecret";
// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
$endpoint = "yourEndpoint";
// Specify the name of the bucket. Example: examplebucket. 
$bucket= "examplebucket";
// Specify the full path of the object. Example: exampledir/exampleobject.jpg. The full path of the object cannot contain the bucket name. 
$object = "exampledir/exampleobject.jpg";
// Specify the local path to which you want to save the image. Example: D:\\localpath\\example-new.jpg. If the specified local file exists, it is overwritten by the processed image. Otherwise, the local file is created. 
// By default, if you set this parameter to the name of a local file such as example-new.jpg without specifying the local path of the file, the processed object is saved to the local path of the project to which the sample program belongs. 
$download_file = "D:\\localpath\\example-new.jpg";

$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, "D:\\localpath\\exampleobject.jpg");

// Use the custom style to process the image. 
// Set yourCustomStyleName to the name of the image style you create 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 the image to your local computer. 
$ossClient->getObject($bucket, $object, $options);

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

Save processed images

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

The following code provides an example on how to save a 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 access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
$accessKeyId = "yourAccessKeyId";
$accessKeySecret = "yourAccessKeySecret";
// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
$endpoint = "yourEndpoint";
// Specify the name of the bucket. Example: examplebucket. 
$bucket= "examplebucket";
// Specify the full path of the source object. Example: exampledir/exampleobject.jpg. The full path of the object cannot contain the bucket name. 
$object = "exampledir/exampleobject.jpg";
// Specify the full path that is used to store the processed image. Example: example-new.jpg. 
$save_object = "example-new.jpg";

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, "D:\\localpath\\exampleobject.jpg");

// After you resize the image to the height and width of 100 pixels, rotate the image 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 the image to the current 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. IMG parameters cannot be added to a signed URL. If you want to process a private object, 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 access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
$accessKeyId = "yourAccessKeyId";
$accessKeySecret = "yourAccessKeySecret";
// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
$endpoint = "yourEndpoint";
// Specify the name of the bucket. Example: examplebucket. 
$bucket= "examplebucket";
// Specify the full path of the object. Example: exampledir/exampleobject.jpg. The full path of the object cannot contain the bucket name. 
$object = "exampledir/exampleobject.jpg";

$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 a browser. 
$timeout = 3600;

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

$signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "GET", $options);
print("rtmp url: \n" . $signedUrl);           
The following code provides an example on how to generate a signed object URL that contains the watermark parameter:
<?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 access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
$accessKeyId = "yourAccessKeyId";
$accessKeySecret = "yourAccessKeySecret";
// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
$endpoint = "yourEndpoint";
// Specify the name of the bucket. Example: examplebucket. 
$bucket= "examplebucket";
// Specify the full path of the object. Example: exampledir/exampleobject.jpg. The full path of the object cannot contain the bucket name. 
$object = "exampledir/exampleobject.jpg";

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

// Generate a signed URL that contains the watermark parameter. The validity period of the URL is 3,600 seconds. The signed URL can be directly accessed by using a browser. 
$timeout = 3600;

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

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

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