curl \
-H "Authorization: Bearer API_KEY" \
-H 'Content-Type: application/json' \
-d '{"mediaUrl": "https://img.ayrshare.com/012/gb.jpg", "platform": "instagram"' \
-X POST https://api.ayrshare.com/api/media/resize
const API_KEY = "API_KEY";
fetch("https://api.ayrshare.com/api/media/resize", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${API_KEY}`
},
body: JSON.stringify({
mediaUrl: "https://img.ayrshare.com/012/gb.jpg", // required
platform: "instagram"
})
})
.then((res) => res.json())
.then((json) => console.log(json))
.catch(console.error);
import requests
payload = {'mediaUrl': 'https://img.ayrshare.com/012/gb.jpg',
'platforms': 'instagram'}
headers = {'Content-Type': 'application/json',
'Authorization': 'Bearer API_KEY'}
r = requests.post('https://api.ayrshare.com/api/media/resize',
json=payload,
headers=headers)
print(r.json())
<?php
$curl = curl_init();
$data = array (
"mediaUrl" => "https://img.ayrshare.com/012/gb.jpg",
"platforms" => "instagram"
);
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.ayrshare.com/api/media/resize',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => http_build_query($data),
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer API_KEY',
'Accept-Encoding: gzip'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
public class AyrshareApiClient
{
private readonly HttpClient _httpClient;
private readonly string _apiKey;
private const string BaseUrl = "https://api.ayrshare.com/api";
public AyrshareApiClient(string apiKey)
{
_apiKey = apiKey ?? throw new ArgumentNullException(nameof(apiKey));
_httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {_apiKey}");
}
public async Task<string> ResizeMediaAsync(string mediaUrl, string platform)
{
try
{
var requestData = new
{
mediaUrl = mediaUrl,
platform = platform
};
var content = new StringContent(
JsonSerializer.Serialize(requestData),
Encoding.UTF8,
"application/json"
);
var response = await _httpClient.PostAsync($"{BaseUrl}/media/resize", content);
response.EnsureSuccessStatusCode();
var jsonResponse = await response.Content.ReadAsStringAsync();
return jsonResponse;
}
catch (HttpRequestException ex)
{
throw new Exception($"Failed to resize media: {ex.Message}", ex);
}
}
public void Dispose()
{
_httpClient.Dispose();
}
}
{
"status": "success",
"url": "https://media.ayrshare.com/9abf1426d6ce9122ef11c72bd62e59807c5cc083/8UbyBjHTxgHkAC1I37e6O.jpg",
"platform": "instagram",
"mode": "blur",
"effects": {
"color": "#A020F0"
}
}
{
"action": "resize",
"status": "error",
"code": 312,
"message": "Invalid extension type. Extension: null. Please verify the extension is one of the following: png, jpg, jpeg and the file is accessible."
}
Media
调整图片尺寸
将图片调整为社交媒体尺寸、添加水印或裁剪
POST
/
media
/
resize
curl \
-H "Authorization: Bearer API_KEY" \
-H 'Content-Type: application/json' \
-d '{"mediaUrl": "https://img.ayrshare.com/012/gb.jpg", "platform": "instagram"' \
-X POST https://api.ayrshare.com/api/media/resize
const API_KEY = "API_KEY";
fetch("https://api.ayrshare.com/api/media/resize", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${API_KEY}`
},
body: JSON.stringify({
mediaUrl: "https://img.ayrshare.com/012/gb.jpg", // required
platform: "instagram"
})
})
.then((res) => res.json())
.then((json) => console.log(json))
.catch(console.error);
import requests
payload = {'mediaUrl': 'https://img.ayrshare.com/012/gb.jpg',
'platforms': 'instagram'}
headers = {'Content-Type': 'application/json',
'Authorization': 'Bearer API_KEY'}
r = requests.post('https://api.ayrshare.com/api/media/resize',
json=payload,
headers=headers)
print(r.json())
<?php
$curl = curl_init();
$data = array (
"mediaUrl" => "https://img.ayrshare.com/012/gb.jpg",
"platforms" => "instagram"
);
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.ayrshare.com/api/media/resize',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => http_build_query($data),
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer API_KEY',
'Accept-Encoding: gzip'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
public class AyrshareApiClient
{
private readonly HttpClient _httpClient;
private readonly string _apiKey;
private const string BaseUrl = "https://api.ayrshare.com/api";
public AyrshareApiClient(string apiKey)
{
_apiKey = apiKey ?? throw new ArgumentNullException(nameof(apiKey));
_httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {_apiKey}");
}
public async Task<string> ResizeMediaAsync(string mediaUrl, string platform)
{
try
{
var requestData = new
{
mediaUrl = mediaUrl,
platform = platform
};
var content = new StringContent(
JsonSerializer.Serialize(requestData),
Encoding.UTF8,
"application/json"
);
var response = await _httpClient.PostAsync($"{BaseUrl}/media/resize", content);
response.EnsureSuccessStatusCode();
var jsonResponse = await response.Content.ReadAsStringAsync();
return jsonResponse;
}
catch (HttpRequestException ex)
{
throw new Exception($"Failed to resize media: {ex.Message}", ex);
}
}
public void Dispose()
{
_httpClient.Dispose();
}
}
{
"status": "success",
"url": "https://media.ayrshare.com/9abf1426d6ce9122ef11c72bd62e59807c5cc083/8UbyBjHTxgHkAC1I37e6O.jpg",
"platform": "instagram",
"mode": "blur",
"effects": {
"color": "#A020F0"
}
}
{
"action": "resize",
"status": "error",
"code": 312,
"message": "Invalid extension type. Extension: null. Please verify the extension is one of the following: png, jpg, jpeg and the file is accessible."
}
各社交网络对社交媒体图片有特定要求。resize 端点允许你选择兼容各社交网络的图片尺寸,添加水印、更换背景、添加特效、裁剪等。
默认情况下,调整尺寸会改变图片的尺寸,但不会裁剪图片。你也可以选择裁剪图片。详见下文。
你也可以使用
必须指定
你也可以使用
必须指定
模糊效果示例图:
位于 southeast 位置的水印示例图:
背景色示例图:
灰度示例图:
示例转换为 WebP:
Header Parameters
Body Parameters
string
必填
待调整尺寸的图片 URL。必须以
https:// 开头。object
以 multipart form-data 对象形式发送媒体文件。当未提供
imageUrl 时必填。string
更改不透明度、颜色等。详见effects options。
object
指定调整尺寸的
width 和 height 的对象。若为裁剪,可选择性地指定中心 x 和 y 坐标。
默认为图片中心。Dimensions
{
"width": 500,
"height": 500,
"xCoordinate": 35, // optional for crop mode
"yCoordinate": 50 // optional for crop mode
}
如果未指定 platform,则 width 和 height 必填。
平台选项
以字符串形式指定平台,即可使用预定义的图片尺寸,你也可以通过dimensions 字段自定义。
例如,"platform": "facebook" 将把图片尺寸设置为宽 1200px、高 630px。
facebook:宽 1200px,高 630px。instagram:宽 1080px,高 1080px。instagram_landscape:宽 1080px,高 680px。instagram_portrait:宽 1080px,高 1920px。instagram_special:宽 1080px,高 800px。linkedin:宽 1200px,高 627px。pinterest:宽 1080px,高 1920px。tiktok:宽 1080px,高 1920px。twitter:宽 1600px,高 900px。
mode 参数设为 crop,并配合 dimensions 以及 xCoordinate、yCoordinate 字段。
Mode
Resize
Resize 是默认模式,会在保持宽高比的同时改变图片尺寸。 将图片调整为指定尺寸,而不裁剪任何内容。 示例 JSON:Resize
{
"mediaUrl": "https://img.ayrshare.com/012/gb.jpg",
"platform": "instagram",
"mode": "resize"
}
dimensions 字段指定自定义尺寸:
Resize with Dimensions
{
"mediaUrl": "https://img.ayrshare.com/012/gb.jpg",
"mode": "resize",
"dimensions": {
"width": 800,
"height": 600
}
}
platform,或者 dimensions 中的 width 和 height。
Crop
Crop 会将图片”裁切”至指定尺寸。默认中心坐标为图片中心。你也可以指定自己的 x/y 坐标。 示例 JSON:Crop
{
"mediaUrl": "https://img.ayrshare.com/012/gb.jpg",
"platform": "instagram",
"mode": "crop"
}
dimensions 字段指定自定义尺寸和可选的裁剪坐标:
Crop with Dimensions
{
"mediaUrl": "https://img.ayrshare.com/012/gb.jpg",
"mode": "crop",
"dimensions": {
"width": 1080,
"height": 1080,
"xCoordinate": 35,
"yCoordinate": 50
}
}
platform,或者 dimensions 中的 width 和 height。
对于方形裁剪,如果 width 或 height 小于所提供图片的尺寸,则会使用 width 或 height 中较小的那个值。例如,若图片为 1200x800 而请求的裁剪尺寸为 1080x1080,返回的图片将为 800x800。
Blur
Blur 效果会复制一份图片作为背景,并对其进行模糊处理。 示例 blur JSON:Blur
{
"mediaUrl": "https://img.ayrshare.com/012/gb.jpg",
"platform": "instagram",
"mode": "blur"
}
Watermark
水印概览
你可以通过提供一个必须以https:// 开头的 URL(以及可选的位置)为图片添加水印。
默认情况下,水印会出现在图片的右下角 —— southeast。
我们建议使用带透明背景的 PNG。
示例 watermark JSON:
Watermark
{
"mediaUrl": "https://img.ayrshare.com/random/photo-13.jpg",
"platform": "instagram",
"watermark": {
"url": "https://img.ayrshare.com/012/100-percent.png",
"position": "northeast" // optional
}
}
水印位置
水印位置可以是以下值之一:northnortheasteastsoutheastsouthsouthwestwestnorthwestcenter
Effects 选项
十六进制颜色
背景模糊颜色的十六进制值。仅当"mode": "blur" 时适用。字符串值,例如 "#A020F0"。
示例背景色 JSON:
Color Background
{
"mediaUrl": "https://img.ayrshare.com/012/gb.jpg",
"platform": "instagram",
"mode": "blur",
"effects": {
"color": "#A020F0"
}
}
颜色:灰度、复古、反色
你可以通过指定grayscale、sepia 或 invert 更改图片的主色调。"blur": true 字段不是必需的,如果你不想要背景,请勿使用。
示例灰度 JSON:
Grayscale
{
"mediaUrl": "https://img.ayrshare.com/random/photo-13.jpg",
"platform": "instagram",
"effects": {
"color": "grayscale"
}
}
不透明度
设置图片的不透明度。数值范围:0 - 1。 示例 opacity JSON:Opacity
{
"effects": {
"opacity": 0.2
}
}
质量
对于 JPG 或 JEPG 图片,可指定图片的质量(即压缩程度)。 数值越低压缩越强,但图片质量越低。 数值越高压缩越弱,但图片质量越高。数值范围:0 - 100。 示例 quality JSON:Quality
{
"effects": {
"quality": 20
}
}
转换为 JPG 或 WebP
convertToJpg 和 convertToWebP 选项允许你将图片从其原始格式(例如 PNG)分别转换为 JPG 或 WebP 格式。
默认情况下,转换后的图片使用 75% 的质量设置。
你可以通过 effects 对象中的 quality 参数自定义压缩级别。
请注意,如果源图片已经是 JPG 且你使用了 convertToJpg,API 只会将图片调整为指定尺寸,而不会改变格式。
示例转换为 JPG:
Convert to JPG
{
"convertToJpg": true
}
Convert to WebP
{
"convertToWebP": true
}
curl \
-H "Authorization: Bearer API_KEY" \
-H 'Content-Type: application/json' \
-d '{"mediaUrl": "https://img.ayrshare.com/012/gb.jpg", "platform": "instagram"' \
-X POST https://api.ayrshare.com/api/media/resize
const API_KEY = "API_KEY";
fetch("https://api.ayrshare.com/api/media/resize", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${API_KEY}`
},
body: JSON.stringify({
mediaUrl: "https://img.ayrshare.com/012/gb.jpg", // required
platform: "instagram"
})
})
.then((res) => res.json())
.then((json) => console.log(json))
.catch(console.error);
import requests
payload = {'mediaUrl': 'https://img.ayrshare.com/012/gb.jpg',
'platforms': 'instagram'}
headers = {'Content-Type': 'application/json',
'Authorization': 'Bearer API_KEY'}
r = requests.post('https://api.ayrshare.com/api/media/resize',
json=payload,
headers=headers)
print(r.json())
<?php
$curl = curl_init();
$data = array (
"mediaUrl" => "https://img.ayrshare.com/012/gb.jpg",
"platforms" => "instagram"
);
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.ayrshare.com/api/media/resize',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => http_build_query($data),
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer API_KEY',
'Accept-Encoding: gzip'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
public class AyrshareApiClient
{
private readonly HttpClient _httpClient;
private readonly string _apiKey;
private const string BaseUrl = "https://api.ayrshare.com/api";
public AyrshareApiClient(string apiKey)
{
_apiKey = apiKey ?? throw new ArgumentNullException(nameof(apiKey));
_httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {_apiKey}");
}
public async Task<string> ResizeMediaAsync(string mediaUrl, string platform)
{
try
{
var requestData = new
{
mediaUrl = mediaUrl,
platform = platform
};
var content = new StringContent(
JsonSerializer.Serialize(requestData),
Encoding.UTF8,
"application/json"
);
var response = await _httpClient.PostAsync($"{BaseUrl}/media/resize", content);
response.EnsureSuccessStatusCode();
var jsonResponse = await response.Content.ReadAsStringAsync();
return jsonResponse;
}
catch (HttpRequestException ex)
{
throw new Exception($"Failed to resize media: {ex.Message}", ex);
}
}
public void Dispose()
{
_httpClient.Dispose();
}
}
{
"status": "success",
"url": "https://media.ayrshare.com/9abf1426d6ce9122ef11c72bd62e59807c5cc083/8UbyBjHTxgHkAC1I37e6O.jpg",
"platform": "instagram",
"mode": "blur",
"effects": {
"color": "#A020F0"
}
}
{
"action": "resize",
"status": "error",
"code": 312,
"message": "Invalid extension type. Extension: null. Please verify the extension is one of the following: png, jpg, jpeg and the file is accessible."
}