Genişlik, yükseklik, başlangıç koordinatları ve sabit nokta konumunu belirterek görüntüleri tam boyutlara kırpın.
Senaryolar
Web tasarımı: Görüntüleri avatarlar, arka planlar ve ürün galerileri gibi düzen öğelerine sığacak şekilde kırpmak için kullanın.
Sosyal medya görüntü teknik özellikleri: Farklı sosyal medya platformları; küçük resimler, gönderi görselleri ve hikaye görselleri dahil olmak üzere çeşitli görüntü türleri için belirli boyut gereksinimleri uygular. En iyi görsel sunumu sağlamak amacıyla görüntülerin önceden önerilen boyutlara kırpılması gerekir.
Mobil uygulama geliştirme: Uygulama simgelerini, açılış ekranlarını ve uygulama içi grafikleri farklı cihaz çözünürlükleriyle eşleşecek şekilde kırpın.
Görüntü veritabanı yönetimi: Kapsamlı görüntü depolarını sürdüren kütüphane ve arşivler gibi kuruluşlarda, kataloglama ve arşivleme iş akışları sırasında görüntü boyutlarını önceden tanımlanmış boyutlara kırpıp standartlaştırmak gerekir.
Kullanım notları
İşlenen görüntünün belirtilen genişliği ve yüksekliği 16.384 pikseli aşmamalıdır.
Başlangıç koordinatları kaynak görüntü sınırlarını aşıyorsa
BadRequesthatası döndürülür: Advance cut's position is out of image.Belirtilen genişlik ve yükseklik kaynak görüntü boyutlarını aşıyorsa kırpma alanı görüntü sınırlarıyla kısıtlanır.
Yöntemler
Görüntü işleme (IMG) parametrelerini yapılandırmak için nesne URL'lerini, OSS SDK'larını kullanabilir veya API operasyonlarını çağırabilirsiniz. Nesne URL'leri yalnızca genel olarak erişilebilir görüntüler (public-read veya public-read-write) için desteklenir. Özel görüntüler için OSS SDK'larını kullanın veya doğrudan API operasyonlarını çağırın. Daha fazla bilgi için bkz. Görüntü işleme yöntemleri .
public-read veya public-read-write görüntüleri kırpın
Görüntü ACL'si public-read veya public-read-write ise işlenen görüntüye erişmek için nesne URL'sine IMG parametreleri ekleyin.
Bu örnekte ?x-oss-process=image/crop,parame_value ifadesi genel okuma izinli bir görüntünün URL'sine eklenmiştir. parame_value değerini yalnızca Parametreler bölümünde açıklanan özel parametreler ve değerlerle değiştirmeniz yeterlidir. Gerektiğinde URL'ye birden çok parametre ekleyebilirsiniz.
|
|
|
|
|
Özel görüntüleri kırpın
OSS SDK'larını kullanın
OSS SDK'larıyla özel bir görüntüyü kırpmaya yönelik örnek kod. Diğer programlama dilleri için bkz. Genel Bakış .
Java
OSS SDK for Java 3.17.4 veya sonraki bir sürüm gerekir.
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 {
// Specify the endpoint of the region. 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 region of the endpoint. 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. Example: examplebucket.
String bucketName = "examplebucket";
// Specify the full path of the source image. Do not include the bucket name in the full path.
String objectName = "example.jpg";
// Specify the full path of the processed image. Example: D:\\dest.jpg. If an image that has the same name already exists in the path, the processed image overwrites the image. Otherwise, the processed image is saved in the path.
String pathName = "D:\\dest.jpg";
// Create an OSSClient instance.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Crop an area of 900 × 900 pixels in the lower-right corner of the image.
String image = "image/crop,w_900,h_900,g_se";
GetObjectRequest request = new GetObjectRequest(bucketName, objectName);
request.setProcess(image);
// Save the processed image to your local computer.
// If you specify only the name of the processed image such as dest.jpg without specifying the local path, the processed image is saved to the local path of the project to which the sample program belongs.
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
OSS SDK for PHP 2.7.0 veya sonraki bir sürüm gerekir.
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$provider = new EnvironmentVariableCredentialsProvider();
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the name of the bucket. Example: examplebucket.
$bucket= "examplebucket";
// Specify the full path of the source image. Do not include the bucket name in the full path.
$object = "src.jpg";
// Specify the full path of the processed image. Example: D:\\dest.jpg. If an image that has the same name already exists in the path, the processed image overwrites the image. Otherwise, the processed image is saved in the path.
// If you specify only the name of the processed image such as dest.jpg without specifying the local path, the processed image is saved to the local path of the project to which the sample program belongs.
$download_file = "D:\\dest.jpg";
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
// Specify the ID of the Alibaba Cloud region in which the bucket is located.
"region" => "cn-hangzhou"
);
$ossClient = new OssClient($config);
// Crop an area of 900 × 900 pixels in the lower-right corner of the image.
$image = "image/crop,w_900,h_900,g_se";
$options = array(
OssClient::OSS_FILE_DOWNLOAD => $download_file,
OssClient::OSS_PROCESS => $image);
// Save the processed image to your local computer.
$ossClient->getObject($bucket, $object, $options);
Python
OSS SDK for Python 2.18.4 veya sonraki bir sürüm gerekir.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# 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.
auth = oss2.ProviderAuthV4(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 ID of the Alibaba Cloud region in which the bucket is located.
region = 'cn-hangzhou'
bucket = oss2.Bucket(auth, endpoint, 'examplebucket', region=region)
# If the source image is stored in the root directory of the bucket, you can specify only the image name. Example: source-example.jpg. If the source image is not stored in the root directory of the bucket, you must specify the full path of the source image. Example: exampledir/source-example.jpg.
key = 'source-example.jpg'
# Specify the full path of the processed image. Example: D:\\target-example.jpg. If an image that has the same name already exists in the path, the processed image overwrites the image. Otherwise, the processed image is saved in the path.
local_file_name = 'D:\\target-example.jpg'
# Configure the crop parameters to crop an area of 900 × 900 pixels in the lower-right corner of the image.
process = 'image/crop,w_900,h_900,g_se'
# Use the get_object method and pass the processing instruction by using the process parameter.
result = bucket.get_object_to_file(key, local_file_name, process=process)
Go
OSS SDK for Go 3.0.2 veya sonraki bir sürüm gerekir.
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 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.
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 in which 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 source image is not stored in the root directory of the bucket, you must specify the full path of the source image. Example: exampledir/example.jpg.
sourceImageName := "example.jpg"
// Specify the name of the processed image.
targetImageName := "D://dest.jpg"
// Crop an area of 900 × 900 pixels in the lower-right corner of the image.
image := "image/crop,w_900,h_900,g_se"
err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(image))
if err != nil {
HandleError(err)
}
}
OSS API'yi kullanın
Doğrudan RESTful API istekleri gönderebilirsiniz. İmza hesaplamasını kodunuza dahil edin. Authorization üst bilgisinin nasıl hesaplanacağı hakkında bilgi için bkz. (Önerilen) V4 imzası ekleme.
GetObject operasyonunda kırpma parametrelerini belirterek görüntüleri ihtiyacınız olan tam boyuta kırpabilirsiniz. Daha fazla bilgi için bkz. GetObject .
GET /oss.jpg?x-oss-process=image/crop,w_900,h_900,g_se HTTP/1.1
Host: oss-example.oss-cn-hangzhou.aliyuncs.com
Date: Fri, 28 Oct 2022 06:40:10 GMT
Authorization: SignatureValue
Parametreler
Eylem: crop
Aşağıdaki tabloda görüntü kırpma parametreleri açıklanmaktadır.
|
Parametre |
Açıklama |
Geçerli değer |
|
w |
Kırpmak istediğiniz alanın genişliği. |
[0, görüntü genişliği] Varsayılan değer: Maksimum değer. |
|
h |
Kırpmak istediğiniz alanın yüksekliği. |
[0, görüntü yüksekliği] Varsayılan değer: Maksimum değer. |
|
x |
Kırpmak istediğiniz alanın X koordinatı. Varsayılan değer, görüntünün sol üst köşesinin X koordinatıdır. |
[0, görüntü genişliği] |
|
y |
Kırpmak istediğiniz alanın Y koordinatı. Varsayılan değer, görüntünün sol üst köşesinin Y koordinatıdır. |
[0, görüntü yüksekliği] |
|
g |
3×3'lük bir ızgaradaki sabit nokta konumu. Görüntü dokuz parçaya bölünür ve kırpma alanı bu konumlardan biriyle hizalanır. |
Her bir parçanın konumunun nasıl hesaplanacağı aşağıdaki tabloda gösterilmiştir. |
|
ratio |
Akıllı kırpma için en boy oranı parametresi. Biçim: |
Hem genişlik hem de yükseklik pozitif tamsayılardır. Not
Bu parametre yalnızca g_auto ayarlandığında etkilidir. Ratio belirtildiğinde algoritma, akıllı şekilde algılanan konuyu koruyarak görüntüyü belirtilen en boy oranına göre kırpar. |
Aşağıdaki tabloda parça konumlarının nasıl hesaplandığı gösterilmektedir. srcW kaynak görüntü genişliği ve srcH kaynak görüntü yüksekliğidir.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|









