Object Storage Service (OSS) içinde depolanan görüntülerin biçimini, çevrimdışı işlemeye gerek kalmadan yalnızca görüntü URL'sine parametreler ekleyerek anında dönüştürebilirsiniz. Bu konu, görüntü biçimi dönüştürme için kullanılan parametreleri açıklar ve örnekler sunar.
Kullanım senaryoları
Cihaz ve platform uyumluluğu: Çeşitli cihazlarda uyumluluğu ve en iyi görüntülemeyi sağlamak için görüntüleri farklı biçimlere dönüştürün.
Depolama maliyetlerini düşürün: WebP gibi bazı görüntü biçimleri, görsel kaliteyi korurken daha küçük dosya boyutlarına sahiptir. Depolama kullanımını azaltmak ve maliyetleri düşürmek için görüntüleri bu biçimlere dönüştürün.
Varlık biçimlerini standartlaştırın: E-ticaret, sosyal medya ve haber uygulamalarında yaygın olduğu üzere, yüklenen tüm görüntüleri tek bir standart biçime (örneğin WebP) dönüştürerek varlık yönetimini ve teslimat işlem hatlarını basitleştirin.
Kullanım notları
Bir görüntü işleme isteği ölçeklendirme işlemi içeriyorsa biçim dönüştürme parametresini işleme parametrelerinin sonuna yerleştirin. Örneğin,
image/resize,w_100/format,jpg.Bir görüntü işleme isteği hem ölçeklendirme hem de filigran işlemleri içeriyorsa biçim dönüştürme parametresini ölçeklendirme parametresinden sonra ekleyin. Örneğin,
image/resize,w_100/format,jpg/watermark,....Alfa kanalı olmayan bir görüntüyü alfa kanalını destekleyen bir biçime (PNG veya WebP gibi) dönüştürdüğünüzde ortaya çıkan şeffaf alanlar varsayılan olarak beyazla doldurulur. OSS'in şeffaf alanı siyahla doldurmayı desteklemediğini unutmayın.
Bir görüntüyü WebP biçimine dönüştürürken görüntünün genişliği ve yüksekliği 16.383 pikseli aşmamalıdır. Aksi takdirde dönüştürme işlemi başarısız olur.
Yöntemler
Görüntü işleme işlemlerini URL parametreleri, SDK'lar veya REST API'leri aracılığıyla yapabilirsiniz. URL tabanlı işleme yalnızca herkese açık görüntüler için kullanılabilir; özel görüntüler için bir SDK kullanmanız veya API'yi doğrudan çağırmanız gerekir. Daha fazla bilgi için bkz. Görüntü işleme yöntemleri.
Public-read images
Herkese açık okuma izinli görüntüler için işleme parametrelerini doğrudan görüntü URL'sine ekleyin. İşlenmiş görüntüye oluşturulan URL kullanılarak erişilebilir.
Herkese açık okuma izinli bir görüntüyü işlemek için görüntü URL'sine ?x-oss-process=image/format,parameter_value parametresini ekleyin. parameter_value ifadesini gerekli parametreler ve değerlerle değiştirin. Birden fazla parametreyi birleştirebilirsiniz.
|
|
|
|
|
Private images
Özel görüntülerin biçimini Alibaba Cloud SDK'ları veya REST API'leri kullanarak dönüştürün.
Use Alibaba Cloud SDKs
Aşağıdaki bölümlerde yaygın SDK'lar kullanılarak görüntü biçimlerinin nasıl dönüştürüleceğini gösteren kod örnekleri sunulmaktadır. Diğer SDK'ları kullanan kod örnekleri için bkz. SDK'ya genel bakış.
Java
Java SDK 3.17.4 veya sonraki bir sürüm gereklidir.
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 {
// The endpoint of the China (Hangzhou) region is used as an example. Specify the actual endpoint.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the region that corresponds to the endpoint, for 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, for example, examplebucket.
String bucketName = "examplebucket";
// Specify the full path of the object. The full path cannot contain the bucket name.
String objectName = "src.gif";
// Specify the full path of the local file, for example, D:\\dest.jpg. If the specified local file exists, it is overwritten. If the file does not exist, it is created.
String pathName = "D:\\dest.png";
// 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 {
// Convert the source image to the PNG format.
String image = "image/format,png";
GetObjectRequest request = new GetObjectRequest(bucketName, objectName);
request.setProcess(image);
// Name the processed image dest.png and save it to a local path.
// If you specify only the file name, such as dest.png, without a local path, the file is saved to the project's corresponding local path by default.
ossClient.getObject(request, new File("D:\\dest.png"));
} 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
PHP SDK 2.7.0 veya sonraki bir sürüm gereklidir.
<?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. The endpoint of the China (Hangzhou) region is used as an example: https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name, for example, examplebucket.
$bucket= "examplebucket";
// Specify the full path of the object. The full path cannot contain the bucket name.
$object = "src.gif";
// Specify the full path of the local file, for example, D:\\dest.png. If the specified local file exists, it is overwritten. If the file does not exist, it is created.
// If you specify only the file name, such as dest.png, without a local path, the file is saved to the project's corresponding local path by default.
$download_file = "D:\\dest.png";
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
// Specify the general-purpose region ID of Alibaba Cloud.
"region" => "cn-hangzhou"
);
$ossClient = new OssClient($config);
// Convert the source image to the PNG format.
$image = "image/format,png";
$options = array(
OssClient::OSS_FILE_DOWNLOAD => $download_file,
OssClient::OSS_PROCESS => $image);
// Save the processed image to a local path.
$ossClient->getObject($bucket, $object, $options);
Python
Python SDK 2.18.4 veya sonraki bir sürüm gereklidir.
# -*- 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 the endpoint to your bucket's endpoint. The endpoint of the China (Hangzhou) region is used as an example: https://oss-cn-hangzhou.aliyuncs.com.
endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'
# Specify the general-purpose region ID of Alibaba Cloud.
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, you must specify the full path, for example, exampledir/src.gif.
key = 'src.gif'
# Specify the name of the processed image.
new_pic = 'D:\\dest.png'
# Convert the source image to the PNG format.
image = 'image/format,png'
bucket.get_object_to_file(key, new_pic, process=image)
Go
Go SDK 3.0.2 veya sonraki bir sürüm gereklidir.
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. The endpoint of the China (Hangzhou) region is used as an example: https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your region.
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, for 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, you must specify the full path, for example, exampledir/src.gif.
sourceImageName := "src.gif"
// Specify the name of the processed image.
targetImageName := "D://dest.png"
// Convert the source image to the PNG format.
image := "image/format,png"
err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(image))
if err != nil {
HandleError(err)
}
}
Use a REST API
İş gereksinimleriniz yüksek düzeyde özelleştirme gerektiriyorsa RESTful API'leri doğrudan çağırın. Bir API'yi doğrudan çağırmak için imza hesaplamasını kodunuza dahil etmeniz gerekir. Authorization başlığının nasıl hesaplanacağı hakkında daha fazla bilgi için bkz. İmza Sürümü 4 (Önerilen).
Bir görüntüyü işlemek için GetObject çağrısında biçim parametrelerini belirtin. Daha fazla bilgi için bkz. GetObject.
GET /oss.jpg?x-oss-process=image/format,png HTTP/1.1
Host: oss-example.oss-cn-hangzhou.aliyuncs.com
Date: Fri, 28 Oct 2022 06:40:10 GMT
Authorization: AuthorizationValue
Parametreler
Eylem: format
|
Değer |
Açıklama |
|
jpg |
Kaynak görüntüyü JPG biçiminde kaydeder. Önemli
Alfa kanalına sahip bir HEIC biçimindeki görüntüyü JPG biçimine kaydedemezsiniz. |
|
png |
Kaynak görüntüyü PNG biçiminde kaydeder. |
|
webp |
Kaynak görüntüyü WebP biçiminde kaydeder. Not
Görüntünün genişliği ve yüksekliği 16.383 pikseli aşmamalıdır. |
|
bmp |
Kaynak görüntüyü BMP biçiminde kaydeder. |
|
gif |
Kaynak görüntü GIF biçimindeyse GIF biçiminde kaydedilir. Kaynak görüntü GIF biçiminde değilse orijinal biçiminde kaydedilir. |
|
tiff |
Kaynak görüntüyü TIFF biçiminde kaydeder. |
|
heic |
Kaynak görüntüyü HEIF biçiminde kaydeder. Not
HEIF biçimi yalnızca Çin (Zhangjiakou), Çin (Şanghay), Çin (Hangzhou), Çin (Shenzhen), Çin (Pekin), Singapur ve ABD (Virginia) bölgelerinde desteklenir. |
|
avif |
Kaynak görüntüyü AVIF biçiminde kaydeder. Not
AVIF biçimi yalnızca Çin (Zhangjiakou), Çin (Şanghay), Çin (Hangzhou), Çin (Shenzhen), Çin (Pekin), Singapur ve ABD (Virginia) bölgelerinde desteklenir. |
Örnekler
Kaynak görüntüyü PNG biçimine dönüştürme
Kaynak görüntüyü JPG biçimine dönüştürme ve kademeli görüntülemeyi etkinleştirme
Kaynak görüntüyü WebP biçimine dönüştürme ve ölçeklendirme
SSS
Dönüştürülen görüntünün kalitesini nasıl kontrol edebilirim?
Her görüntü biçiminin varsayılan bir kalite ayarı vardır. Biçim dönüştürme sırasında kalite parametresini belirtmezseniz OSS bu varsayılan değeri uygular.
Görüntü kalitesini artırmak istiyorsanız biçim dönüştürme sırasında kaliteyi 100 olarak ayarlayın; örneğin ?x-oss-process=image/quality,Q_100 kullanın. Kalite dönüştürme hakkında daha fazla bilgi için bkz. Kalite dönüştürme.
Görüntü biçimi dönüştürme işlemi sayfa yükleme hızını etkiler mi?
Görüntü biçimi dönüştürme anında yapılır. İlk istekte hafif bir işleme gecikmesi yaşanabilir ancak elde edilen daha küçük ve daha verimli görüntü biçimi (WebP gibi) genellikle sonraki yükleme sürelerini hızlandırır ve genel sayfa performansını iyileştirir.
Bir GIF görüntüsünü MP4 biçimine dönüştürebilir miyim?
Bir GIF görüntüsünü MP4 biçimine dönüştürmek için bu özelliği talep etmek amacıyla bir destek bileti oluşturun.



