Certaines images peuvent contenir des données au format Exchangeable Image File Format (EXIF), incluant des attributs d'image et des paramètres de prise de vue. Les données EXIF comprennent des informations telles que le taux de compression, l'orientation, la résolution horizontale et la résolution verticale. Pour obtenir les données EXIF d'une image, ajoutez le paramètre info à l'URL de l'image.
Paramètres
Action : info
Les informations renvoyées sont au format JSON. Chaque champ de la réponse utilise la structure "FieldName": {"value": "field value"} et toutes les valeurs de champ sont des chaînes de caractères.
|
Champ |
Description |
Toujours renvoyé |
|
|
Taille du fichier image, en octets. |
Oui |
|
|
Format de l'image, par exemple jpg, png ou gif. |
Oui |
|
|
Nombre d'images dans l'image. |
Oui |
|
|
Hauteur de l'image, en pixels. |
Oui |
|
|
Largeur de l'image, en pixels. |
Oui |
|
|
Unité de résolution de l'image. Renvoyé uniquement si l'image contient des métadonnées de résolution. |
Non |
|
|
Résolution horizontale de l'image, renvoyée sous forme de fraction, telle que 1/1 ou 96/1. Renvoyé uniquement si l'image contient des métadonnées de résolution. |
Non |
|
|
Résolution verticale de l'image, renvoyée sous forme de fraction, telle que 1/1 ou 96/1. Renvoyé uniquement si l'image contient des métadonnées de résolution. |
Non |
|
Champs EXIF, tels que |
Données EXIF de l'image. Les champs renvoyés dépendent des données EXIF effectivement présentes dans l'image. Pour plus d'informations, consultez Exif Version 2.31. |
Non |
Vous ne pouvez pas utiliser l'action info conjointement avec d'autres actions dans une même requête de traitement d'image. Par exemple, vous ne pouvez pas utiliser image/resize,s_700,limit_0/info pour redimensionner une image et interroger simultanément les informations de l'image redimensionnée.
Limites
L'action info permet d'interroger les informations uniquement pour les images JPG, PNG, BMP, GIF, WebP, TIFF et HEIC.
Bien que le format PNG soit pris en charge, vous ne pouvez pas utiliser l'action info pour interroger les données EXIF des images PNG.
Opérations
Configurez les paramètres de traitement d'image à l'aide d'URL, de SDK OSS ou d'API RESTful. Les URL conviennent aux images accessibles publiquement. Nous vous recommandons d'utiliser les SDK OSS ou les API RESTful pour les images privées. Pour plus de détails, consultez Méthodes de traitement d'image.
Obtenir des informations sur les images public-read ou public-read-write
Pour obtenir des informations sur les images public-read ou public-read-write, ajoutez directement les paramètres de traitement d'image aux URL.
-
Interroger les informations d'une image ne contenant pas de données EXIF
https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/info
Lorsque vous ajoutez le paramètre info, les champs
FileSize,Format,FrameCount,ImageHeightetImageWidthconstituent les informations de base de l'image et sont renvoyés pour chaque image prise en charge. Les champsResolutionUnit,XResolutionetYResolutionsont renvoyés uniquement si l'image contient des métadonnées de résolution. Tous les autres champs EXIF sont renvoyés uniquement si l'image contient des données EXIF. L'image de l'exemple suivant ne contient pas de données EXIF, mais elle comporte des métadonnées de résolution. Par conséquent, la réponse inclut les champs de résolution en plus des informations de base.{ "FileSize": {"value": "21839"}, "Format": {"value": "jpg"}, "FrameCount": {"value": "1"}, "ImageHeight": {"value": "267"}, "ImageWidth": {"value": "400"}, "ResolutionUnit": {"value": "1"}, "XResolution": {"value": "1/1"}, "YResolution": {"value": "1/1"} } -
Interroger les informations d'une image contenant des données EXIF
https://image-demo.oss-cn-hangzhou.aliyuncs.com/f.jpg?x-oss-process=image/info
Si vous ajoutez le paramètre info à l'URL d'une image contenant des données EXIF, la réponse inclut les informations de base de l'image ainsi que ses données EXIF. Pour plus d'informations, consultez Exif Version 2.31.
{ "Compression": {"value": "6"}, "DateTime": {"value": "2015:02:11 15:38:27"}, "ExifTag": {"value": "2212"}, "FileSize": {"value": "23471"}, "Format": {"value": "jpg"}, "FrameCount": {"value": "1"}, "GPSLatitude": {"value": "0deg "}, "GPSLatitudeRef": {"value": "North"}, "GPSLongitude": {"value": "0deg "}, "GPSLongitudeRef": {"value": "East"}, "GPSMapDatum": {"value": "WGS-84"}, "GPSTag": {"value": "4292"}, "GPSVersionID": {"value": "2 2 0 0"}, "ImageHeight": {"value": "333"}, "ImageWidth": {"value": "424"}, "JPEGInterchangeFormat": {"value": "4518"}, "JPEGInterchangeFormatLength": {"value": "3232"}, "Orientation": {"value": "7"}, "ResolutionUnit": {"value": "2"}, "Software": {"value": "Microsoft Windows Photo Viewer 6.1.7600.16385"}, "XResolution": {"value": "96/1"}, "YResolution": {"value": "96/1"}}
Obtenir des informations sur les images privées
Pour obtenir des informations sur les images privées, utilisez le SDK Alibaba Cloud ou l'API REST.
Utiliser le SDK Alibaba Cloud
L'exemple de code suivant montre comment utiliser les SDK pour les langages de programmation courants afin d'obtenir des informations sur les images. Pour savoir comment obtenir des informations sur les images à l'aide des SDK pour d'autres langages, consultez Référence du SDK.
Java
La version 3.17.4 ou ultérieure du SDK Java est requise.
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyuncs.exceptions.ClientException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class Demo{
public static void main(String[] args) throws ClientException, ClientException {
// yourEndpoint Specify the endpoint of the region in which the bucket is located.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the endpoint of the region. For example, cn-hangzhou.
String region = "cn-hangzhou";
// 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.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the name of the bucket.
String bucketName = "examplebucket";
// Specify the name of the image if it is stored in the root directory of the bucket. If it is stored elsewhere, you must specify the full path of the image. Example: exampledir/example.jpg.
String key = "example.jpg";
// Create an OSSClient instance.
// Call the shutdown method to release resources when the OSSClient is no longer in use.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Create a request to obtain information about the image.
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
getObjectRequest.setProcess("image/info");
// Use getObject and input instructions via process parameters.
OSSObject ossObject = ossClient.getObject(getObjectRequest);
// Read and print information.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = ossObject.getObjectContent().read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
String imageInfo = baos.toString("UTF-8");
System.out.println("Image Info:");
System.out.println(imageInfo);
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
} finally {
// Shut down OSSClient
ossClient.shutdown();
}
}
}
PHP
La version 2.7.0 ou ultérieure du SDK PHP est requise.
<?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;
try {
// 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 = 'https://oss-cn-hangzhou.aliyuncs.com';
// Specify the name of the bucket. Example: examplebucket.
$bucket = 'examplebucket';
// Specify the name of the image if it is stored in the root directory of the bucket. If it is stored elsewhere, you must specify the full path of the image. Example: exampledir/example.jpg.
$key = 'example.jpg';
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
// Specify the Region ID.
"region" => "cn-hangzhou"
);
$ossClient = new OssClient($config);
// Create a request to obtain information about the image.
$options[$ossClient::OSS_PROCESS] = "image/info";
$result = $ossClient->getObject($bucket,$key,$options);
var_dump($result);
} catch (OssException $e) {
printf($e->getMessage() . "\n");
return;
}
Go
La version 3.0.2 ou ultérieure du SDK Go est requise.
package main
import (
"fmt"
"io"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// 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, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// 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. Specify your actual endpoint.
// Specify the Region ID. Example: cn-hangzhou.
client, err := oss.New("https://oss-cn-hangzhou.aliyuncs.com", "", "", oss.SetCredentialsProvider(&provider), oss.AuthVersion(oss.AuthV4), oss.Region("cn-hangzhou"))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Specify the name of the bucket. Example: examplebucket.
bucketName := "examplebucket"
bucket, err := client.Bucket(bucketName)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Specify the name of the image if it is stored in the root directory of the bucket. If it is stored elsewhere, you must specify the full path of the image. Example: exampledir/example.jpg.
// Create a request to obtain information about the image by using oss.Process.
body, err := bucket.GetObject("example.jpg", oss.Process("image/info"))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
defer body.Close()
data, err := io.ReadAll(body)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Println("data:", string(data))
}
Utiliser l'API RESTful
Si votre activité nécessite un niveau élevé de personnalisation, vous pouvez appeler directement les API RESTful. Pour appeler directement une API, vous devez intégrer le calcul de signature dans votre code. Pour plus d'informations, consultez (Recommandé) Inclure une signature V4.
Ajoutez des paramètres IMG ou des paramètres de style d'image aux requêtes GetObject pour traiter les images.
GET /oss.jpg?x-oss-process=image/info HTTP/1.1
Host: oss-example.oss-cn-hangzhou.aliyuncs.com
Date: Fri, 28 Oct 2022 06:40:10 GMT
Authorization: OSS4-HMAC-SHA256 Credential=LTAI********************/20250417/cn-hangzhou/oss/aliyun_v4_request,Signature=a7c3554c729d71929e0b84489addee6b2e8d5cb48595adfc51868c299c0c218e
FAQ
Comment interroger les informations d'une image privée ?
Pour interroger les informations d'une image privée, ajoutez le paramètre info à une URL signée à l'aide des SDK OSS, puis utilisez cette URL pour accéder à l'image.
-
Générez une URL signée.
L'exemple de code suivant montre comment utiliser le SDK OSS pour Java afin de générer une URL signée :
import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.model.GeneratePresignedUrlRequest; import java.net.URL; import java.util.Date; public class Demo { public static void main(String[] args) throws Throwable { // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. String region = "cn-hangzhou"; // For security purposes, we recommend that you do not save access credentials in the project code. In this example, access credentials are obtained from environment variables. Before you run the sample code, make sure that the environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the name of the bucket. Example: examplebucket. String bucketName = "examplebucket"; // Specify the full path of the object. Do not include the bucket name in the full path. String objectName = "exampleobject.jpg"; // Create an OSSClient instance. // Call the shutdown method to release resources when the OSSClient is no longer in use. ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); // Explicitly declare the use of the V4 signature algorithm. clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { // Query information about the image object. String style = "image/info"; // Set the validity period of the signed URL to 10 minutes. Date expiration = new Date(new Date().getTime() + 1000 * 60 * 10 ); GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethod.GET); req.setExpiration(expiration); req.setProcess(style); URL signedUrl = ossClient.generatePresignedUrl(req); System.out.println(signedUrl); } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } } Utilisez l'URL signée pour accéder à l'image depuis un navigateur et interroger les informations de l'image.