Tous les produits
Search
Centre de documentation

Object Storage Service:Flou

Dernière mise à jour :Aug 08, 2026

Vous pouvez appliquer un flou à une image entière ou à une partie d’une image dans Object Storage Service (OSS) afin de protéger des informations sensibles ou d’améliorer le rendu visuel.

Scénarios

  • Protection des informations sensibles : avant de publier une image contenant des données sensibles, telles qu’une plaque d’immatriculation ou un visage, appliquez un flou partiel pour masquer ces éléments.

  • Composition d’images : lors de la combinaison de plusieurs calques d’image, ajoutez un effet de flou pour adoucir les transitions entre les calques. Le rendu visuel gagne ainsi en fluidité.

  • Masquage d’une faible résolution : si une image présente une faible résolution et ne se prête pas à un affichage haute définition, appliquez un flou modéré pour atténuer l’aspect pixélisé.

Instructions

Méthodes

Vous pouvez configurer les paramètres de traitement d'image à l'aide d'une URL de fichier, d'un kit SDK ou d'une API. La méthode par URL de fichier s'applique uniquement aux images accessibles publiquement. Pour les images privées, vous devez utiliser un SDK ou une API. Pour plus d'informations, consultez la rubrique Méthodes de traitement d'image.

Images accessibles publiquement

Pour les images disposant des autorisations public-read ou public-read-write, vous pouvez ajouter directement les paramètres de traitement à l'URL de l'image. Toute personne peut alors accéder en permanence et de manière anonyme à l'image traitée.

L'exemple suivant illustre l'ajout du paramètre ?x-oss-process=image/blur,parameter_value à une URL d'image. Remplacez parameter_value par les paramètres et valeurs spécifiques décrits dans la section Paramètres. Vous pouvez également combiner plusieurs paramètres.

URL de l'image source

URL de l'image avec paramètres de traitement

https://oss-console-img-demo-cn-hangzhou-3az.oss-cn-hangzhou.aliyuncs.com/example1.jpg

https://oss-console-img-demo-cn-hangzhou-3az.oss-cn-hangzhou.aliyuncs.com/example1.jpg?x-oss-process=image/blur,r_10,s_10

Images privées

Utiliser un SDK Alibaba Cloud

Les exemples de code suivants montrent comment ajouter un effet de flou aux images privées à l'aide des SDK courants. Pour des exemples utilisant d'autres SDK, consultez la rubrique Présentation des SDK.

Java

Le SDK Java version 3.17.4 ou ultérieure est requis.

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.GetObjectRequest;
import java.io.File;

public class Demo {
    public static void main(String[] args) throws Throwable {
        // In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Specify the region that corresponds to the endpoint. Example: cn-hangzhou.
        String region = "cn-hangzhou";
        // Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the bucket name. Example: examplebucket.
        String bucketName = "examplebucket";
        // Specify the full path of the object. The full path cannot contain the bucket name.
        String objectName = "src.jpg";
        // Specify the full path of the local file. Example: D:\\localpath\\example-new.jpg. If the specified local file exists, it is overwritten. Otherwise, a new file is created.
        String pathName = "D:\\dest.jpg";

        // Create an OSSClient instance.
        // When the OSSClient instance is no longer used, call the shutdown method to release the resources.
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();

        try {
            // Apply a blur to the image with a radius of 10 and a standard deviation of 10.
            String image = "image/blur,r_10,s_10";
            GetObjectRequest request = new GetObjectRequest(bucketName, objectName);
            request.setProcess(image);
            // Name the processed image dest.jpg and save it to your local machine.
            // If you specify only the file name (for example, dest.jpg) without a local path, the file is saved to the local path of the project by default.
            ossClient.getObject(request, new File("D:\\dest.jpg"));
        } 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();
            }
        }
    }
}

PHP

Le SDK PHP version 2.7.0 ou ultérieure est requis.

<?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 this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$provider = new EnvironmentVariableCredentialsProvider();
// Set yourEndpoint to the endpoint of the region where the bucket is located. In this example, the endpoint of the China (Hangzhou) region is used.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name. Example: examplebucket.
$bucket= "examplebucket";
// Specify the full path of the object. Example: exampledir/exampleobject.jpg. The full path cannot contain the bucket name.
$object = "src.jpg";
// Specify the full path of the local file. Example: D:\\localpath\\example-new.jpg. If the specified local file exists, it is overwritten. Otherwise, a new file is created.
// If you specify only the local file name (for example, example-new.jpg) without a local path, the file is saved to the local path of the project by default.
$download_file = "D:\\dest.jpg";

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,        
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        // Specify the general-purpose Alibaba Cloud region ID.
        "region" => "cn-hangzhou"
    );
$ossClient = new OssClient($config);

// Apply a blur to the image with a radius of 10 and a standard deviation of 10.
$image = "image/blur,r_10,s_10";

$options = array(
    OssClient::OSS_FILE_DOWNLOAD => $download_file,
    OssClient::OSS_PROCESS => $image);

// Save the processed image to your local machine.
$ossClient->getObject($bucket, $object, $options);                           

Python

Le SDK Python version 2.18.4 ou ultérieure est requis.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Set yourEndpoint to the endpoint of the region where the bucket is located. In this example, the endpoint of the China (Hangzhou) region is used.
# Specify the endpoint of the region where the bucket is located. In this example, the endpoint of the China (Hangzhou) region is used.
endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'
# Specify the general-purpose Alibaba Cloud region ID.
region = 'cn-hangzhou'
bucket = oss2.Bucket(auth, endpoint, 'examplebucket', region=region)
# Specify the name of the source image. If the image is not in the root directory of the bucket, specify the full path of the image, such as exampledir/example.jpg.
key = 'src.jpg'
# Specify the name of the processed image.
new_pic = 'D:\\dest.jpg'

# Apply a blur to the image with a radius of 10 and a standard deviation of 10.
image = 'image/blur,r_10,s_10'
bucket.get_object_to_file(key, new_pic, process=image)

Go

Le SDK Go version 3.0.2 ou ultérieure est requis.

package main

import (
	"fmt"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func HandleError(err error) {
	fmt.Println("Error:", err)
	os.Exit(-1)
}

func main() {
	// Obtain access credentials from environment variables. Before you run this 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.
	// Set yourEndpoint to the endpoint of the bucket. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
	client, err := oss.New("https://oss-cn-hangzhou.aliyuncs.com", "", "", oss.SetCredentialsProvider(&provider), oss.AuthVersion(oss.AuthV4), oss.Region("cn-hangzhou"))
	if err != nil {
		HandleError(err)
	}

	// Specify the name of the bucket where the source image is stored. Example: examplebucket.
	bucketName := "examplebucket"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		HandleError(err)
	}

	// Specify the name of the source image. If the image is not in the root directory of the bucket, specify the full path of the image, such as exampledir/example.jpg.
	sourceImageName := "src.jpg"
	// Specify the name of the processed image.
	targetImageName := "D://dest.jpg"
	// Apply a blur to the image with a radius of 10 and a standard deviation of 10.
	image := "image/blur,r_10,s_10"
	err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(image))
	if err != nil {
		HandleError(err)
	}
}

Utiliser une API REST

Si votre application nécessite un niveau élevé de personnalisation, vous pouvez envoyer des requêtes d'API REST directement. Cela implique d'écrire manuellement le code pour calculer la signature. Pour plus d'informations sur le calcul de l'en-tête de requête Authorization, consultez la rubrique Signature V4 (recommandée).

Vous pouvez ajouter des paramètres de flou à l'opération GetObject pour traiter une image. Pour plus d'informations, consultez la rubrique GetObject.

GET /oss.jpg?x-oss-process=image/blur,r_10,s_10 HTTP/1.1
Host: oss-example.oss-cn-hangzhou.aliyuncs.com
Date: Fri, 28 Oct 2022 06:40:10 GMT
Authorization: SignatureValue

Images privées (nécessite la liaison d'un projet IMM)

Pour utiliser le paramètre g_face ou g_faces, vous devez d'abord lier le bucket à un projet Intelligent Media Management (IMM). Pour plus d'informations, consultez la rubrique Démarrage rapide.

Utiliser un SDK Alibaba Cloud

Les exemples de code suivants montrent comment ajouter un effet de flou aux images privées à l'aide des SDK courants. Pour des exemples utilisant d'autres SDK, consultez la rubrique Présentation des SDK.

Java

Le SDK Java version 3.17.4 ou ultérieure est requis.

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.GetObjectRequest;
import java.io.File;

public class Demo {
    public static void main(String[] args) throws Throwable {
        // In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Specify the region that corresponds to the endpoint. Example: cn-hangzhou.
        String region = "cn-hangzhou";
        // Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the bucket name. Example: examplebucket.
        String bucketName = "examplebucket";
        // Specify the full path of the source image. The full path cannot contain the bucket name.
        String objectName = "example.jpg";
        // Specify the full path to which you want to save the processed image. Example: D:\\dest.jpg. If the specified local file exists, it is overwritten. Otherwise, a new file is created.
        String pathName = "D:\\dest.jpg";

        // Create an OSSClient instance.
        // When the OSSClient instance is no longer used, call the shutdown method to release the resources.
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();

        try {
            // Set the blur scope to all faces, and apply a blur with a radius of 25 and a standard deviation of 50.
            String image = "image/blur,g_faces,r_25,s_50";
            GetObjectRequest request = new GetObjectRequest(bucketName, objectName);
            request.setProcess(image);
            // Save the processed image to your local machine.
            // If you specify only the file name (for example, dest.jpg) without a local path, the file is saved to the local path of the project by default.
            ossClient.getObject(request, new File("D:\\dest.jpg"));
        } 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();
            }
        }
    }
}

PHP

Le SDK PHP version 2.7.0 ou ultérieure est requis.

<?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 this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$provider = new EnvironmentVariableCredentialsProvider();
// Set yourEndpoint to the endpoint of the region where the bucket is located. In this example, the endpoint of the China (Hangzhou) region is used.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name. Example: examplebucket.
$bucket= "examplebucket";
// Specify the full path of the source image. The full path cannot contain the bucket name.
$object = "src.jpg";
// Specify the full path to which you want to save the processed image. Example: D:\\dest.jpg. If the specified local file exists, it is overwritten. Otherwise, a new file is created.
// If you specify only the local file name (for example, dest.jpg) without a local path, the file is saved to the local path of the project by default.
$download_file = "D:\\dest.jpg";

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,        
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        // Specify the general-purpose Alibaba Cloud region ID.
        "region" => "cn-hangzhou"
    );
$ossClient = new OssClient($config);

// Set the blur scope to all faces, and apply a blur with a radius of 25 and a standard deviation of 50.
$image = "image/blur,g_faces,r_25,s_50";

$options = array(
    OssClient::OSS_FILE_DOWNLOAD => $download_file,
    OssClient::OSS_PROCESS => $image);

// Save the processed image to your local machine.
$ossClient->getObject($bucket, $object, $options);                           

Python

Le SDK Python version 2.18.4 ou ultérieure est requis.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region where the bucket is located. In this example, the endpoint of the China (Hangzhou) region is used.
endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'
# Specify the general-purpose Alibaba Cloud region ID.
region = 'cn-hangzhou'
bucket = oss2.Bucket(auth, endpoint, 'examplebucket', region=region)

# If the source image is in the root directory of the bucket, specify only the image name, such as source-example.jpg. If the source image is not in the root directory, specify the full path, such as exampledir/source-example.jpg.
key = 'source-example.jpg'

# Specify the full path of the local file. Example: D:\\target-example.jpg. If the specified local file exists, it is overwritten. Otherwise, a new file is created.
local_file_name = 'D:\\target-example.jpg'

# Set the blur scope to all faces, and apply a blur with a radius of 25 and a standard deviation of 50.
process = 'image/blur,g_faces,r_25,s_50'

# Use the get_object method and pass the processing instruction in the process parameter.
result = bucket.get_object_to_file(key, local_file_name, process=process)

Go

Le SDK Go version 3.0.2 ou ultérieure est requis.

package main

import (
	"fmt"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func HandleError(err error) {
	fmt.Println("Error:", err)
	os.Exit(-1)
}

func main() {
	// Obtain access credentials from environment variables. Before you run this 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.
	// Set yourEndpoint to the endpoint of the bucket. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
	client, err := oss.New("https://oss-cn-hangzhou.aliyuncs.com", "", "", oss.SetCredentialsProvider(&provider), oss.AuthVersion(oss.AuthV4), oss.Region("cn-hangzhou"))
	if err != nil {
		HandleError(err)
	}

	// Specify the name of the bucket where the source image is stored. Example: examplebucket.
	bucketName := "examplebucket"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		HandleError(err)
	}

	// Specify the name of the source image. If the image is not in the root directory of the bucket, specify the full path of the image, such as exampledir/example.jpg.
	sourceImageName := "example.jpg"
	// Specify the name of the processed image.
	targetImageName := "D://dest.jpg"
	// Set the blur scope to all faces, and apply a blur with a radius of 25 and a standard deviation of 50.
	image := "image/blur,g_faces,r_25,s_50"
	err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(image))
	if err != nil {
		HandleError(err)
	}
}

Utiliser une API REST

Si votre application nécessite un niveau élevé de personnalisation, vous pouvez envoyer des requêtes d'API REST directement. Cela implique d'écrire manuellement le code pour calculer la signature. Pour plus d'informations sur le calcul de l'en-tête de requête Authorization, consultez la rubrique Signature V4 (recommandée).

Vous pouvez ajouter des paramètres de flou à l'opération GetObject pour traiter une image. Pour plus d'informations, consultez la rubrique GetObject.

GET /oss.jpg?x-oss-process=image/blur,g_faces,r_25,s_50 HTTP/1.1
Host: oss-example.oss-cn-hangzhou.aliyuncs.com
Date: Fri, 28 Oct 2022 06:40:10 GMT
Authorization: SignatureValue

Paramètres

Action : blur

Le tableau suivant décrit les paramètres.

Paramètre

Obligatoire

Description

Valeurs valides

r

Oui

Rayon de flou.

[1, 50]

Plus la valeur est élevée, plus l'image est floue.

s

Oui

Écart type de la distribution normale.

[1, 50]

Plus la valeur est élevée, plus l'image est floue.

g

Non

Portée du flou.

  • face : le visage le plus grand de l'image.

  • faces : tous les visages de l'image.

Remarque
  • Vous devez d'abord associer un projet IMM. Pour associer un projet dans la console, consultez la section Démarrage rapide. Pour associer un projet à l'aide d'une API, consultez la section AttachOSSBucket.

  • L'accès anonyme n'est pas pris en charge.

  • Vous devez disposer des autorisations requises pour le traitement IMM. Pour plus d'informations, consultez la section Autorisations.

p

Non

Facteur de mise à l'échelle.

[1, 200]

Unité : pourcentage.

Remarque

Ce paramètre n'est effectif que lorsque g_face ou g_faces est défini.

Exemples

Appliquer un flou à l'ensemble de l'image

Les paramètres de traitement sont les suivants :

  • Opération de flou : blur

  • Appliquer un flou avec un rayon de 10 et un écart type de 10 : r_10,s_10

Pour une image accessible publiquement, vous pouvez ajouter ?x-oss-process=image/blur,r_10,s_10 à la fin de l'URL de l'image. OSS traite l'image en temps réel, applique un flou avec un rayon de 10 et un écart type de 10, puis renvoie le résultat. Pour appliquer un flou à l'ensemble d'une image privée, consultez la section Images privées.

Exemple

L'exemple suivant montre comment appliquer un flou avec un rayon de 10 et un écart type de 10 en ajoutant le paramètre ?x-oss-process=image/blur,r_10,s_10 à l'URL de l'image source :

Image source

Image traitée

原图

模糊1

URL de l'image source : https://oss-console-img-demo-cn-hangzhou-3az.oss-cn-hangzhou.aliyuncs.com/example1.jpg

URL de traitement de l'image : https://oss-console-img-demo-cn-hangzhou-3az.oss-cn-hangzhou.aliyuncs.com/example1.jpg?x-oss-process=image/blur,r_10,s_10

Appliquer un flou au visage le plus grand

Les paramètres de traitement sont les suivants :

  • Opération de flou : blur

  • Définir la zone de flou sur le visage le plus grand : g_face

  • Appliquer un flou avec un rayon de 25 et un écart type de 50 : r_25,s_50

Vous pouvez traiter l'image à l'aide d'un SDK. Vous devez d'abord lier un projet IMM. Le code suivant présente un exemple :

Exemple de code

Java

Le SDK Java version 3.17.4 ou ultérieure est requis.

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.GetObjectRequest;
import java.io.File;

public class Demo {
    public static void main(String[] args) throws Throwable {
        // In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Specify the region that corresponds to the endpoint. Example: cn-hangzhou.
        String region = "cn-hangzhou";
        // Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the bucket name. Example: examplebucket.
        String bucketName = "examplebucket";
        // Specify the full path of the source image. The full path cannot contain the bucket name.
        String objectName = "example.jpg";
        // Specify the full path to which you want to save the processed image. Example: D:\\dest.jpg. If the specified local file exists, it is overwritten. Otherwise, a new file is created.
        String pathName = "D:\\dest.jpg";

        // Create an OSSClient instance.
        // When the OSSClient instance is no longer used, call the shutdown method to release the resources.
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();

        try {
            // Apply a blur to the largest face in the image with a radius of 25 and a standard deviation of 50.
            String image = "image/blur,g_face,r_25,s_50";
            GetObjectRequest request = new GetObjectRequest(bucketName, objectName);
            request.setProcess(image);
            // Save the processed image to your local machine.
            // If you specify only the file name (for example, dest.jpg) without a local path, the file is saved to the local path of the project by default.
            ossClient.getObject(request, new File("D:\\dest.jpg"));
        } 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();
            }
        }
    }
}

PHP

Le SDK PHP version 2.7.0 ou ultérieure est requis.

<?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 this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$provider = new EnvironmentVariableCredentialsProvider();
// Set yourEndpoint to the endpoint of the region where the bucket is located. In this example, the endpoint of the China (Hangzhou) region is used.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name. Example: examplebucket.
$bucket= "examplebucket";
// Specify the full path of the source image. The full path cannot contain the bucket name.
$object = "src.jpg";
// Specify the full path to which you want to save the processed image. Example: D:\\dest.jpg. If the specified local file exists, it is overwritten. Otherwise, a new file is created.
// If you specify only the local file name (for example, dest.jpg) without a local path, the file is saved to the local path of the project by default.
$download_file = "D:\\dest.jpg";

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,        
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        // Specify the general-purpose Alibaba Cloud region ID.
        "region" => "cn-hangzhou"
    );
$ossClient = new OssClient($config);

// Apply a blur to the largest face in the image with a radius of 25 and a standard deviation of 50.
$image = "image/blur,g_face,r_25,s_50";

$options = array(
    OssClient::OSS_FILE_DOWNLOAD => $download_file,
    OssClient::OSS_PROCESS => $image);

// Save the processed image to your local machine.
$ossClient->getObject($bucket, $object, $options);                           

Python

Le SDK Python version 2.18.4 ou ultérieure est requis.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region where the bucket is located. In this example, the endpoint of the China (Hangzhou) region is used.
endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'
# Specify the general-purpose Alibaba Cloud region ID.
region = 'cn-hangzhou'
bucket = oss2.Bucket(auth, endpoint, 'examplebucket', region=region)

# If the source image is in the root directory of the bucket, specify only the image name, such as source-example.jpg. If the source image is not in the root directory, specify the full path, such as exampledir/source-example.jpg.
key = 'source-example.jpg'

# Specify the full path of the local file. Example: D:\\target-example.jpg. If the specified local file exists, it is overwritten. Otherwise, a new file is created.
local_file_name = 'D:\\target-example.jpg'

# Apply a blur to the largest face in the image with a radius of 25 and a standard deviation of 50.
process = 'image/blur,g_face,r_25,s_50'

# Use the get_object method and pass the processing instruction in the process parameter.
result = bucket.get_object_to_file(key, local_file_name, process=process)

Go

Le SDK Go version 3.0.2 ou ultérieure est requis.

package main

import (
	"fmt"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func HandleError(err error) {
	fmt.Println("Error:", err)
	os.Exit(-1)
}

func main() {
	// Obtain access credentials from environment variables. Before you run this 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.
	// Set yourEndpoint to the endpoint of the bucket. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
	client, err := oss.New("https://oss-cn-hangzhou.aliyuncs.com", "", "", oss.SetCredentialsProvider(&provider), oss.AuthVersion(oss.AuthV4), oss.Region("cn-hangzhou"))
	if err != nil {
		HandleError(err)
	}

	// Specify the name of the bucket where the source image is stored. Example: examplebucket.
	bucketName := "examplebucket"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		HandleError(err)
	}

	// Specify the name of the source image. If the image is not in the root directory of the bucket, specify the full path of the image, such as exampledir/example.jpg.
	sourceImageName := "example.jpg"
	// Specify the name of the processed image.
	targetImageName := "D://dest.jpg"
	// Apply a blur to the largest face in the image with a radius of 25 and a standard deviation of 50.
	image := "image/blur,g_face,r_25,s_50"
	err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(image))
	if err != nil {
		HandleError(err)
	}
}

Exemple

L'exemple suivant utilise le paramètre image/blur,g_face,r_25,s_50 pour appliquer un flou au visage le plus grand d'une image, avec un rayon de 25 et un écart type de 50 :

Image source

Image traitée

原图

image

Flouter tous les visages

Les paramètres de traitement sont les suivants :

  • Opération de flou : blur

  • Définissez la portée du flou sur tous les visages : g_faces

  • Appliquez un flou avec un rayon de 25 et un écart type de 50 : r_25,s_50

Vous pouvez traiter l'image à l'aide d'un SDK. Vous devez d'abord lier un projet IMM. Le code suivant présente un exemple :

Exemple de code

Java

Le SDK Java 3.17.4 ou une version ultérieure est requis.

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.GetObjectRequest;
import java.io.File;

public class Demo {
    public static void main(String[] args) throws Throwable {
        // In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Specify the region that corresponds to the endpoint. Example: cn-hangzhou.
        String region = "cn-hangzhou";
        // Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the bucket name. Example: examplebucket.
        String bucketName = "examplebucket";
        // Specify the full path of the source image. The full path cannot contain the bucket name.
        String objectName = "example.jpg";
        // Specify the full path to which you want to save the processed image. Example: D:\\dest.jpg. If the specified local file exists, it is overwritten. Otherwise, a new file is created.
        String pathName = "D:\\dest.jpg";

        // Create an OSSClient instance.
        // When the OSSClient instance is no longer used, call the shutdown method to release the resources.
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();

        try {
            // Apply a blur to all faces in the image with a radius of 25 and a standard deviation of 50.
            String image = "image/blur,g_faces,r_25,s_50";
            GetObjectRequest request = new GetObjectRequest(bucketName, objectName);
            request.setProcess(image);
            // Save the processed image to your local machine.
            // If you specify only the file name (for example, dest.jpg) without a local path, the file is saved to the local path of the project by default.
            ossClient.getObject(request, new File("D:\\dest.jpg"));
        } 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();
            }
        }
    }
}

PHP

Le SDK PHP 2.7.0 ou une version ultérieure est requis.

<?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 this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$provider = new EnvironmentVariableCredentialsProvider();
// Set yourEndpoint to the endpoint of the region where the bucket is located. In this example, the endpoint of the China (Hangzhou) region is used.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name. Example: examplebucket.
$bucket= "examplebucket";
// Specify the full path of the source image. The full path cannot contain the bucket name.
$object = "src.jpg";
// Specify the full path to which you want to save the processed image. Example: D:\\dest.jpg. If the specified local file exists, it is overwritten. Otherwise, a new file is created.
// If you specify only the local file name (for example, dest.jpg) without a local path, the file is saved to the local path of the project by default.
$download_file = "D:\\dest.jpg";

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,        
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        // Specify the general-purpose Alibaba Cloud region ID.
        "region" => "cn-hangzhou"
    );
$ossClient = new OssClient($config);

// Apply a blur to all faces in the image with a radius of 25 and a standard deviation of 50.
$image = "image/blur,g_faces,r_25,s_50";

$options = array(
    OssClient::OSS_FILE_DOWNLOAD => $download_file,
    OssClient::OSS_PROCESS => $image);

// Save the processed image to your local machine.
$ossClient->getObject($bucket, $object, $options);                           

Python

Le SDK Python 2.18.4 ou une version ultérieure est requis.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region where the bucket is located. In this example, the endpoint of the China (Hangzhou) region is used.
endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'
# Specify the general-purpose Alibaba Cloud region ID.
region = 'cn-hangzhou'
bucket = oss2.Bucket(auth, endpoint, 'examplebucket', region=region)

# If the source image is in the root directory of the bucket, specify only the image name, such as source-example.jpg. If the source image is not in the root directory, specify the full path, such as exampledir/source-example.jpg.
key = 'source-example.jpg'

# Specify the full path of the local file. Example: D:\\target-example.jpg. If the specified local file exists, it is overwritten. Otherwise, a new file is created.
local_file_name = 'D:\\target-example.jpg'

# Apply a blur to all faces in the image with a radius of 25 and a standard deviation of 50.
process = 'image/blur,g_faces,r_25,s_50'

# Use the get_object method and pass the processing instruction in the process parameter.
result = bucket.get_object_to_file(key, local_file_name, process=process)

Go

Le SDK Go 3.0.2 ou une version ultérieure est requis.

package main

import (
	"fmt"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func HandleError(err error) {
	fmt.Println("Error:", err)
	os.Exit(-1)
}

func main() {
	// Obtain access credentials from environment variables. Before you run this 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.
	// Set yourEndpoint to the endpoint of the bucket. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
	client, err := oss.New("https://oss-cn-hangzhou.aliyuncs.com", "", "", oss.SetCredentialsProvider(&provider), oss.AuthVersion(oss.AuthV4), oss.Region("cn-hangzhou"))
	if err != nil {
		HandleError(err)
	}

	// Specify the name of the bucket where the source image is stored. Example: examplebucket.
	bucketName := "examplebucket"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		HandleError(err)
	}

	// Specify the name of the source image. If the image is not in the root directory of the bucket, specify the full path of the image, such as exampledir/example.jpg.
	sourceImageName := "example.jpg"
	// Specify the name of the processed image.
	targetImageName := "D://dest.jpg"
	// Apply a blur to all faces in the image with a radius of 25 and a standard deviation of 50.
	image := "image/blur,g_faces,r_25,s_50"
	err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(image))
	if err != nil {
		HandleError(err)
	}
}

Exemples

Dans l'exemple suivant, le paramètre image/blur,g_faces,r_25,s_50 applique un flou à tous les visages d'une image avec un rayon de flou de 25 et un écart type de 50 :

Image source

Image traitée

原图

image

Flouter le plus grand visage et mettre à l'échelle la zone floutée à 200 %

Les paramètres de traitement sont les suivants :

  • Opération de flou : blur

  • Définissez la portée du flou sur le plus grand visage : g_face

  • Mettez à l'échelle la zone floutée à 200 % : p_200

  • Appliquez un flou avec un rayon de 25 et un écart type de 50 : r_25,s_50

Vous pouvez traiter l'image à l'aide d'un SDK. Vous devez d'abord lier un projet IMM. Le code suivant présente un exemple :

Exemple de code

Java

Le SDK Java 3.17.4 ou une version ultérieure est requis.

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.GetObjectRequest;
import java.io.File;

public class Demo {
    public static void main(String[] args) throws Throwable {
        // In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Specify the region that corresponds to the endpoint. Example: cn-hangzhou.
        String region = "cn-hangzhou";
        // Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the bucket name. Example: examplebucket.
        String bucketName = "examplebucket";
        // Specify the full path of the source image. The full path cannot contain the bucket name.
        String objectName = "example.jpg";
        // Specify the full path to which you want to save the processed image. Example: D:\\dest.jpg. If the specified local file exists, it is overwritten. Otherwise, a new file is created.
        String pathName = "D:\\dest.jpg";

        // Create an OSSClient instance.
        // When the OSSClient instance is no longer used, call the shutdown method to release the resources.
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();

        try {
            // Apply a blur to the largest face with a radius of 25 and a standard deviation of 50, and scale the blurred area by 200%.
            String image = "image/blur,g_face,p_200,r_25,s_50";
            GetObjectRequest request = new GetObjectRequest(bucketName, objectName);
            request.setProcess(image);
            // Save the processed image to your local machine.
            // If you specify only the file name (for example, dest.jpg) without a local path, the file is saved to the local path of the project by default.
            ossClient.getObject(request, new File("D:\\dest.jpg"));
        } 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();
            }
        }
    }
}

PHP

Le SDK PHP 2.7.0 ou une version ultérieure est requis.

<?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 this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$provider = new EnvironmentVariableCredentialsProvider();
// Set yourEndpoint to the endpoint of the region where the bucket is located. In this example, the endpoint of the China (Hangzhou) region is used.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name. Example: examplebucket.
$bucket= "examplebucket";
// Specify the full path of the source image. The full path cannot contain the bucket name.
$object = "src.jpg";
// Specify the full path to which you want to save the processed image. Example: D:\\dest.jpg. If the specified local file exists, it is overwritten. Otherwise, a new file is created.
// If you specify only the local file name (for example, dest.jpg) without a local path, the file is saved to the local path of the project by default.
$download_file = "D:\\dest.jpg";

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,        
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        // Specify the general-purpose Alibaba Cloud region ID.
        "region" => "cn-hangzhou"
    );
$ossClient = new OssClient($config);

// Apply a blur to the largest face with a radius of 25 and a standard deviation of 50, and scale the blurred area by 200%.
$image = "image/blur,g_face,p_200,r_25,s_50";

$options = array(
    OssClient::OSS_FILE_DOWNLOAD => $download_file,
    OssClient::OSS_PROCESS => $image);

// Save the processed image to your local machine.
$ossClient->getObject($bucket, $object, $options);                           

Python

Le SDK Python 2.18.4 ou une version ultérieure est requis.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region where the bucket is located. In this example, the endpoint of the China (Hangzhou) region is used.
endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'
# Specify the general-purpose Alibaba Cloud region ID.
region = 'cn-hangzhou'
bucket = oss2.Bucket(auth, endpoint, 'examplebucket', region=region)

# If the source image is in the root directory of the bucket, specify only the image name, such as source-example.jpg. If the source image is not in the root directory, specify the full path, such as exampledir/source-example.jpg.
key = 'source-example.jpg'

# Specify the full path of the local file. Example: D:\\target-example.jpg. If the specified local file exists, it is overwritten. Otherwise, a new file is created.
local_file_name = 'D:\\target-example.jpg'

# Apply a blur to the largest face with a radius of 25 and a standard deviation of 50, and scale the blurred area by 200%.
process = 'image/blur,g_face,p_200,r_25,s_50'

# Use the get_object method and pass the processing instruction in the process parameter.
result = bucket.get_object_to_file(key, local_file_name, process=process)

Go

Le SDK Go 3.0.2 ou une version ultérieure est requis.

package main

import (
	"fmt"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func HandleError(err error) {
	fmt.Println("Error:", err)
	os.Exit(-1)
}

func main() {
	// Obtain access credentials from environment variables. Before you run this 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.
	// Set yourEndpoint to the endpoint of the bucket. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
	client, err := oss.New("https://oss-cn-hangzhou.aliyuncs.com", "", "", oss.SetCredentialsProvider(&provider), oss.AuthVersion(oss.AuthV4), oss.Region("cn-hangzhou"))
	if err != nil {
		HandleError(err)
	}

	// Specify the name of the bucket where the source image is stored. Example: examplebucket.
	bucketName := "examplebucket"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		HandleError(err)
	}

	// Specify the name of the source image. If the image is not in the root directory of the bucket, specify the full path of the image, such as exampledir/example.jpg.
	sourceImageName := "example.jpg"
	// Specify the name of the processed image.
	targetImageName := "D://dest.jpg"
	// Apply a blur to the largest face with a radius of 25 and a standard deviation of 50, and scale the blurred area by 200%.
	image := "image/blur,g_face,p_200,r_25,s_50"
	err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(image))
	if err != nil {
		HandleError(err)
	}
}

Exemples

L'exemple suivant utilise le paramètre image/blur,g_face,p_200,r_25,s_50 pour appliquer un flou au plus grand visage d'une image avec un rayon de 25 et un écart type de 50, et agrandir la zone de traitement à 200 % :

Image source

Image traitée

原图

image

Appliquer un flou à tous les visages et définir une zone agrandie 2x

Les paramètres de traitement sont les suivants :

  • Opération de flou : blur

  • Définir la portée du flou sur tous les visages : g_faces

  • Mettre à l'échelle la zone floutée à 200 % : p_200

  • Appliquer un flou avec un rayon de 25 et un écart type de 50 : r_25,s_50

Vous pouvez traiter l'image à l'aide d'un SDK. Vous devez d'abord lier un projet IMM. Le code suivant présente un exemple :

Exemple de code

Java

Le SDK Java 3.17.4 ou une version ultérieure est requis.

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.GetObjectRequest;
import java.io.File;

public class Demo {
    public static void main(String[] args) throws Throwable {
        // In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Specify the region that corresponds to the endpoint. Example: cn-hangzhou.
        String region = "cn-hangzhou";
        // Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the bucket name. Example: examplebucket.
        String bucketName = "examplebucket";
        // Specify the full path of the source image. The full path cannot contain the bucket name.
        String objectName = "example.jpg";
        // Specify the full path to which you want to save the processed image. Example: D:\\dest.jpg. If the specified local file exists, it is overwritten. Otherwise, a new file is created.
        String pathName = "D:\\dest.jpg";

        // Create an OSSClient instance.
        // When the OSSClient instance is no longer used, call the shutdown method to release the resources.
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();

        try {
            // Apply a blur to all faces with a radius of 25 and a standard deviation of 50, and scale the blurred area by 200%.
            String image = "image/blur,g_faces,p_200,r_25,s_50";
            GetObjectRequest request = new GetObjectRequest(bucketName, objectName);
            request.setProcess(image);
            // Save the processed image to your local machine.
            // If you specify only the file name (for example, dest.jpg) without a local path, the file is saved to the local path of the project by default.
            ossClient.getObject(request, new File("D:\\dest.jpg"));
        } 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();
            }
        }
    }
}

PHP

Le SDK PHP 2.7.0 ou une version ultérieure est requis.

<?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 this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$provider = new EnvironmentVariableCredentialsProvider();
// Set yourEndpoint to the endpoint of the region where the bucket is located. In this example, the endpoint of the China (Hangzhou) region is used.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name. Example: examplebucket.
$bucket= "examplebucket";
// Specify the full path of the source image. The full path cannot contain the bucket name.
$object = "src.jpg";
// Specify the full path to which you want to save the processed image. Example: D:\\dest.jpg. If the specified local file exists, it is overwritten. Otherwise, a new file is created.
// If you specify only the local file name (for example, dest.jpg) without a local path, the file is saved to the local path of the project by default.
$download_file = "D:\\dest.jpg";

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,        
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        // Specify the general-purpose Alibaba Cloud region ID.
        "region" => "cn-hangzhou"
    );
$ossClient = new OssClient($config);

// Apply a blur to all faces with a radius of 25 and a standard deviation of 50, and scale the blurred area by 200%.
$image = "image/blur,g_faces,p_200,r_25,s_50";

$options = array(
    OssClient::OSS_FILE_DOWNLOAD => $download_file,
    OssClient::OSS_PROCESS => $image);

// Save the processed image to your local machine.
$ossClient->getObject($bucket, $object, $options);                           

Python

Le SDK Python 2.18.4 ou une version ultérieure est requis.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region where the bucket is located. In this example, the endpoint of the China (Hangzhou) region is used.
endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'
# Specify the general-purpose Alibaba Cloud region ID.
region = 'cn-hangzhou'
bucket = oss2.Bucket(auth, endpoint, 'examplebucket', region=region)

# If the source image is in the root directory of the bucket, specify only the image name, such as source-example.jpg. If the source image is not in the root directory, specify the full path, such as exampledir/source-example.jpg.
key = 'source-example.jpg'

# Specify the full path of the local file. Example: D:\\target-example.jpg. If the specified local file exists, it is overwritten. Otherwise, a new file is created.
local_file_name = 'D:\\target-example.jpg'

# Apply a blur to all faces with a radius of 25 and a standard deviation of 50, and scale the blurred area by 200%.
process = 'image/blur,g_faces,p_200,r_25,s_50'

# Use the get_object method and pass the processing instruction in the process parameter.
result = bucket.get_object_to_file(key, local_file_name, process=process)

Go

Le SDK Go 3.0.2 ou une version ultérieure est requis.

package main

import (
	"fmt"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func HandleError(err error) {
	fmt.Println("Error:", err)
	os.Exit(-1)
}

func main() {
	// Obtain access credentials from environment variables. Before you run this 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.
	// Set yourEndpoint to the endpoint of the bucket. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
	client, err := oss.New("https://oss-cn-hangzhou.aliyuncs.com", "", "", oss.SetCredentialsProvider(&provider), oss.AuthVersion(oss.AuthV4), oss.Region("cn-hangzhou"))
	if err != nil {
		HandleError(err)
	}

	// Specify the name of the bucket where the source image is stored. Example: examplebucket.
	bucketName := "examplebucket"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		HandleError(err)
	}

	// Specify the name of the source image. If the image is not in the root directory of the bucket, specify the full path of the image, such as exampledir/example.jpg.
	sourceImageName := "example.jpg"
	// Specify the name of the processed image.
	targetImageName := "D://dest.jpg"
	// Apply a blur to all faces with a radius of 25 and a standard deviation of 50, and scale the blurred area by 200%.
	image := "image/blur,g_faces,p_200,r_25,s_50"
	err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(image))
	if err != nil {
		HandleError(err)
	}
}

Exemple

Le paramètre image/blur,g_faces,p_200,r_25,s_50 suivant applique un flou à tous les visages d'une image avec un rayon de 25 et un écart type de 50, et agrandit la zone de flou à 200 % :

Source image

Processed image

原图

image

Références

Vous pouvez appliquer des opérations de traitement d'image directement à une URL d'image. Vous pouvez également utiliser des feuilles de style en cascade (CSS) pour ajuster davantage le style et optimiser l'effet visuel. Le code suivant montre comment utiliser la fonctionnalité de traitement d'image OSS pour flouter une image et la combiner avec du CSS pour un effet visuel plus riche.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Blur Effect Example</title>
    <style>
        .blurred {
            filter: blur(5px);
        }

        .container {
            position: relative;
            width: 300px;
        }

        .text {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            color: white;
            font-size: 24px;
        }
    </style>
</head>
<body>
    <div class="container">
        <img src="https://oss-console-img-demo-cn-hangzhou-3az.oss-cn-hangzhou.aliyuncs.com/example1.jpg" alt="Sample Image" class="blurred">
        <div class="text">Hello World</div>
    </div>
    <div>
        <img src="https://oss-console-img-demo-cn-hangzhou-3az.oss-cn-hangzhou.aliyuncs.com/example1.jpg?x-oss-process=image%2Fblur%2Cr_10%2Cs_10&spm=a2c4g.11186623.0.i12">
    </div>
</body>
</html>