Image Processing (IMG) は、イメージの処理を支援するためにObject Storage service (OSS) によって提供される、効率的で費用対効果の高い信頼性の高いイメージ処理サービスです。 ソース画像をOSSにアップロードした後、RESTful APIを呼び出して、いつでもどこでもインターネットに接続されているデバイスで画像を処理できます。
使用上の注意
このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。 OSSと同じリージョンにある他のAlibaba CloudサービスからOSSにアクセスする場合は、内部エンドポイントを使用します。 OSSリージョンとエンドポイントの詳細については、「リージョン、エンドポイント、オープンポート」をご参照ください。
このトピックでは、OSSエンドポイントを使用してOSSClientインスタンスを作成します。 カスタムドメイン名またはSecurity Token Service (STS) を使用してOSSClientを作成する場合は、「OSSClientインスタンスの作成」をご参照ください。
IMGパラメータを使用した画像処理
単一のIMGパラメータを使用して画像を処理する
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account that is used to access OSS. */
/* 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. */
std::string Endpoint = "yourEndpoint";
/* Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the name of the bucket in which the source image is stored. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the name of the source image. If the image is not stored in the root directory of the bucket, you must specify the full path of the image. Example: exampledir/example.jpg. */
std::string ObjectName = "exampledir/example.jpg";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 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. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Resize the image to a height and width of 100 pixels and save the image to your local computer. */
std::string Process = "image/resize,m_fixed,w_100,h_100";
GetObjectRequest request(BucketName, ObjectName);
request.setProcess(Process);
auto outcome = client.GetObject(request);
if (outcome.isSuccess()) {
std::cout << "Image processed successfully." << std::endl;
} else {
std::cout << "Failed to process image. Error code: " << outcome.error().Code()
<< ", Message: " << outcome.error().Message()
<< ", RequestId: " << outcome.error().RequestId() << std::endl;
}
/* Release resources such as network resources. */
ShutdownSdk();
return 0;
}
複数のIMGパラメータを使用して画像を処理する
次のコードでは、複数のIMGパラメーターを使用してイメージを処理する方法の例を示します。 IMGパラメータはスラッシュ (/) で区切られます。
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account that is used to access OSS. */
/* 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. */
std::string Endpoint = "yourEndpoint";
/* Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the name of the bucket in which the source image is stored. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the name of the source image. If the image is not stored in the root directory of the bucket, you must specify the full path of the image. Example: exampledir/example.jpg. */
std::string ObjectName = "exampledir/example.jpg";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 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. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Resize the image to a height and width of 100 pixels, rotate the image 90 degrees, and save the image to your local computer. */
std::string Process = "image/resize,m_fixed,w_100,h_100/rotate,90";
GetObjectRequest request(BucketName, ObjectName);
request.setProcess(Process);
auto outcome = client.GetObject(request);
if (outcome.isSuccess()) {
std::cout << "Image processed successfully." << std::endl;
} else {
std::cout << "Failed to process image. Error code: " << outcome.error().Code()
<< ", Message: " << outcome.error().Message()
<< ", RequestId: " << outcome.error().RequestId() << std::endl;
}
/* Release resources such as network resources. */
ShutdownSdk();
return 0;
}
画像スタイルを使用して画像を処理する
イメージスタイルを作成します。
複数のIMGパラメータをイメージスタイルに追加して、バケットに保存されているイメージに対して複雑な操作を実行できます。 詳細については、「イメージスタイル」をご参照ください。
イメージスタイルを使用してイメージを処理します。
#include <alibabacloud/oss/OssClient.h> using namespace AlibabaCloud::OSS; int main(void) { /* Initialize information about the account that is used to access OSS. */ /* 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. */ std::string Endpoint = "yourEndpoint"; /* Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. */ std::string Region = "yourRegion"; /* Specify the name of the bucket in which the source image is stored. Example: examplebucket. */ std::string BucketName = "examplebucket"; /* Specify the name of the source image. If the image is not stored in the root directory of the bucket, you must specify the full path of the image. Example: exampledir/example.jpg. */ std::string ObjectName = "exampledir/example.jpg"; /* Initialize resources such as network resources. */ InitializeSdk(); ClientConfiguration conf; conf.signatureVersion = SignatureVersionType::V4; /* 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. */ auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>(); OssClient client(Endpoint, credentialsProvider, conf); client.SetRegion(Region); /* Use the image style to process the image. Replace yourCustomStyleName with the name of the image style that you created in Step 1. */ std::string Process = "style/yourCustomStyleName"; GetObjectRequest request(BucketName, ObjectName); request.setProcess(Process); auto outcome = client.GetObject(request); if (outcome.isSuccess()) { std::cout << "Image processed successfully." << std::endl; } else { std::cout << "Failed to process image. Error code: " << outcome.error().Code() << ", Message: " << outcome.error().Message() << ", RequestId: " << outcome.error().RequestId() << std::endl; } /* Release resources such as network resources. */ ShutdownSdk(); return 0; }
処理された画像を保存する
ImgSaveAs操作を呼び出して、ソースイメージが保存されているバケットに処理されたイメージを保存できます。 次のコードは、処理されたイメージを保存する方法の例を示します。
#include <alibabacloud/oss/OssClient.h>
#include <sstream>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account that is used to access OSS. */
/* 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. */
std::string Endpoint = "yourEndpoint";
/* Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the name of the source image. If the image is not stored in the root directory of the bucket, precede the image name with the path to the image. Example: example/example.jpg. */
std::string SourceObjectName = "example/example.jpg";
/* Specify the name of the processed image. If the image is not stored in the root directory of the bucket, precede the image name with the path to the image. Example: exampledir/example.jpg. */
std::string TargetObjectName = "exampledir/example.jpg";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 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. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Resize the image to a height and width of 100 pixels and save the image to the bucket in which the source image is stored. */
std::string Process = "image/resize,m_fixed,w_100,h_100";
std::stringstream ss;
ss << Process
<<"|sys/saveas"
<< ",o_" << Base64EncodeUrlSafe(TargetObjectName)
<< ",b_" << Base64EncodeUrlSafe(BucketName);
ProcessObjectRequest request(BucketName, SourceObjectName, ss.str());
auto outcome = client.ProcessObject(request);
if (outcome.isSuccess()) {
std::cout << "Image processed successfully." << std::endl;
} else {
std::cout << "Failed to process image. Error code: " << outcome.error().Code()
<< ", Message: " << outcome.error().Message()
<< ", RequestId: " << outcome.error().RequestId() << std::endl;
}
/* Release resources such as network resources. */
ShutdownSdk();
return 0;
}
IMGパラメータを含む署名付きオブジェクトURLを生成する
プライベートオブジェクトのURLに署名する必要があります。 署名付きURLの末尾にIMGパラメーターを追加することはできません。 プライベートオブジェクトを処理する場合は、署名にIMGパラメーターを追加します。 サンプルコード:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account that is used to access OSS. */
/* 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. */
std::string Endpoint = "yourEndpoint";
/* Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the name of the bucket in which the image is located. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the name of the image. If the image is not stored in the root directory of the bucket, precede the image name with the path to the image. Example: exampledir/example.jpg. */
std::string ObjectName = "exampledir/example.jpg";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 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. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Generate a signed URL that includes IMG parameters for the object. */
std::string Process = "image/resize,m_fixed,w_100,h_100";
GeneratePresignedUrlRequest request(BucketName, ObjectName, Http::Get);
request.setProcess(Process);
/* The maximun validity period of the URL is 32400s. */
auto outcome = client.GeneratePresignedUrl(request);
if (outcome.isSuccess()) {
std::cout << "Generated presigned URL: " << outcome.result() << std::endl;
} else {
std::cout << "Failed to generate presigned URL. Error code: " << outcome.error().Code()
<< ", Message: " << outcome.error().Message()
<< ", RequestId: " << outcome.error().RequestId() << std::endl;
}
/* Release resources such as network resources. */
ShutdownSdk();
return 0;
}