O processamento síncrono (x-oss-process) exige que o programa aguarde a conclusão de uma tarefa antes de executar outras. Use o PHP SDK V2 para esse tipo de processamento em cenários como tratamento de imagens e documentos.
Precauções
O código de exemplo usa a região China (Hangzhou) (
cn-hangzhou) e o endpoint público por padrão. Para acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região, use o endpoint interno. Para obter mais informações sobre o mapeamento entre regiões e endpoints, consulte Regiões e endpoints do OSS.
Código de exemplo
O exemplo a seguir usa o PHP SDK V2 para redimensionar uma imagem e salvar o resultado em um bucket específico para persistência.
<?php
// Import the autoloader file to load dependency libraries.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define the command line argument descriptions.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region where the bucket is located. This parameter is required.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint that other services can use to access OSS. This parameter is optional.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // The name of the bucket. This parameter is required.
"key" => ['help' => 'The name of the object', 'required' => True], // The name of the object. This parameter is required.
];
// Generate a list of long options to parse command line arguments.
$longopts = \array_map(function ($key) {
return "$key:"; // Add a colon after each parameter to indicate that a value is required.
}, array_keys($optsdesc));
// Parse the command line arguments.
$options = getopt("", $longopts);
// Check whether required parameters are missing.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help"; // Prompt the user that a required parameter is missing.
exit(1);
}
}
// Obtain the command line argument values.
$region = $options["region"]; // The region where the bucket is located.
$bucket = $options["bucket"]; // The name of the bucket.
$key = $options["key"]; // The name of the object.
// Load credentials, such as AccessKeyId and AccessKeySecret, from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Set the credentials provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Set the region.
$cfg->setRegion($region);
// If an endpoint is provided, set the endpoint.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSS client instance.
$client = new Oss\Client($cfg);
// Construct an image processing command to resize the image width to 100 and save the image as a new object.
$process = sprintf("image/resize,w_100|sys/saveas,o_%s",
base64_encode($key) // Base64-encode the object name.
);
// Create a request object to process the object.
$request = new Oss\Models\ProcessObjectRequest(
bucket: $bucket, // The name of the bucket.
key: $key // The name of the object.
);
// Set the processing command.
$request->process = $process;
// Call the processObject method to process the object.
$result = $client->processObject($request);
// Print the result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code of the response.
'request id:' . $result->requestId . PHP_EOL . // The unique ID of the request.
'process result:' . var_export($result, true) . PHP_EOL // The processing result.
);
Cenários
Adicionar marca d'água invisível a uma imagem
Referências
Para obter mais informações sobre os parâmetros de processamento de imagens, consulte Parâmetros de processamento de imagens.
Para obter mais informações sobre o recurso de processamento síncrono, consulte Processamento síncrono.