Image Processing (IMG) は、イメージの処理を支援するためにObject Storage service (OSS) によって提供される、安全で費用対効果の高い信頼性の高いイメージ処理サービスです。 ソースイメージをOSSにアップロードした後、RESTful APIを呼び出して、いつでもどこでも、接続されたデバイスでイメージを処理できます。
使用上の注意
このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。 OSSと同じリージョンにある他のAlibaba CloudサービスからOSSにアクセスする場合は、内部エンドポイントを使用します。 OSSリージョンとエンドポイントの詳細については、「リージョン、エンドポイント、オープンポート」をご参照ください。
このトピックでは、OSSエンドポイントを使用してOSSClientインスタンスを作成します。 カスタムドメイン名またはSecurity Token Service (STS) を使用してOSSClientインスタンスを作成する場合は、「OSSClientインスタンスの作成」をご参照ください。
OSSでは、1つ以上のIMGパラメータを使用してイメージを処理できます。 詳細は、「IMGパラメーター」をご参照ください。 完全なサンプルコードについては、『GitHub』をご参照ください。
IMGパラメータを使用した画像処理
単一のIMGパラメータを使用して画像を処理し、処理した画像をローカルコンピュータに保存する
<?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 the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint 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. Do not include the bucket name in the full path. $object = "exampledir/exampleobject.jpg"; // Specify the full path to which you want to save the processed image. Example: D:\\localpath\\example-resize.jpg. If a file that has the same name already exists, the downloaded object overwrites the file. Otherwise, the downloaded object is saved in the path. // If you specify only the name of the file such as example-resize.jpg but do not specify the local path, the file is saved to the path of the project to which the sample program belongs. $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 image that you want to process does not exist in the specified bucket, you must upload the image to the specified bucket. // $ossClient->uploadFile($bucket, $object, "D:\\localpath\\exampleobject.jpg"); // Resize the image to 100 x 100 pixels. $options = array( OssClient::OSS_FILE_DOWNLOAD => $download_file, OssClient::OSS_PROCESS => "image/resize,m_fixed,h_100,w_100" ); // Save the processed image as example-resize.jpg 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);
複数のIMGパラメータを使用して画像を処理し、処理した画像をローカルコンピュータに保存する
次のサンプルコードは、複数のIMGパラメーターを使用してイメージを処理する方法の例を示しています。 IMGパラメータは、スラッシュ (/) で区切られます。
<?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 the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. $provider = new EnvironmentVariableCredentialsProvider(); // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint 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. Do not include the bucket name in the full path. $object = "exampledir/exampleobject.jpg"; // Specify the full path to which you want to save the processed image. Example: D:\\localpath\\example-new.jpg. If a file that has the same name already exists, the downloaded object overwrites the file. Otherwise, the downloaded object is saved in the path. // If you specify only the name of the file such as example-new.jpg but do not specify the local path, the file is saved to the path of the project to which the sample program belongs. $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 image that you want to process does not exist in the specified bucket, you must upload the image to the specified bucket. // $ossClient->uploadFile($bucket, $object, "D:\\localpath\\exampleobject.jpg"); // Resize the image to 100 × 100 pixels and 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); // Save the processed image as example-new.jpg 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);
画像スタイルを使用して画像を処理する
OSSコンソールでイメージスタイルを作成し、そのスタイルに複数のIMGパラメーターをカプセル化できます。 次に、スタイルを使用して画像を処理できます。 詳細については、「イメージスタイル」をご参照ください。
次のサンプルコードは、イメージスタイルを使用してイメージを処理する方法の例を示しています。
<?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 the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$provider = new EnvironmentVariableCredentialsProvider();
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint 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. Do not include the bucket name in the full path.
$object = "exampledir/exampleobject.jpg";
// Specify the full path to which you want to save the processed image. Example: D:\\localpath\\example-new.jpg. If a file that has the same name already exists, the downloaded object overwrites the file. Otherwise, the downloaded object is saved in the path.
// If you specify only the name of the file such as example-new.jpg but do not specify the local path, the file is saved to the path of the project to which the sample program belongs.
$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 image that you want to process does not exist in the specified bucket, you must upload the image to the specified bucket.
// $ossClient->uploadFile($bucket, $object, "D:\\localpath\\exampleobject.jpg");
// Use the custom image style to process the image.
// 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);
// Save the processed image as example-new.jpg 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);
処理された画像を保存する
デフォルトでは、IMGは処理済み画像を保存しません。 ImgSaveAs操作を呼び出して、ソースイメージが保存されているバケットにイメージを保存できます。
次のサンプルコードは、処理されたイメージを保存する方法の例を示します。
<?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 the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$provider = new EnvironmentVariableCredentialsProvider();
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint 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. Do not include the bucket name in the full path.
$object = "exampledir/exampleobject.jpg";
// Specify the full path to which you want to save the processed image. 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 image that you want to process does not exist in the specified bucket, you must upload the image to the specified bucket.
// $ossClient->uploadFile($bucket, $object, "D:\\localpath\\exampleobject.jpg");
// Resize the image to 100 × 100 pixels and 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);
// Save the processed image as example-new.png to the current bucket.
$result = $ossClient->processObject($bucket, $object, $process);
// Display the processing result.
print($result);
IMGパラメータを含む署名付きオブジェクトURLを生成する
プライベートオブジェクトのURLに署名する必要があります。 署名付きURLの末尾にIMGパラメーターを直接追加することはできません。 プライベートオブジェクトを処理する場合は、署名にIMGパラメーターを追加します。 次のサンプルコードは、署名にIMGパラメーターを追加する方法の例を示しています。
<?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 the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$provider = new EnvironmentVariableCredentialsProvider();
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint 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. Do not include the bucket name in the full path.
$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 IMG parameters. The validity period of the URL is 3,600 seconds. The signed URL can be directly used to access the image by using a browser.
$timeout = 3600;
$options = array(
// Resize the image to 100 x 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);
次のサンプルコードは、透かしパラメータを含む署名付きオブジェクトURLを生成する方法の例を示しています。
<?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 the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$provider = new EnvironmentVariableCredentialsProvider();
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint 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. Do not include the bucket name in the full path.
$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 the watermark parameter. The validity period of the URL is 3,600 seconds. The signed URL can be directly used to access the image 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 the 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.
OssClient::OSS_PROCESS => "image/watermark,text_".$string
);
$signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "GET", $options);
print("rtmp url: \n" . $signedUrl);