Tài liệu API Upload Ảnh
Hướng dẫn sử dụng API upload ảnh (hỗ trợ upload file và URL)
Tổng quan API
API này cho phép upload ảnh từ file hoặc URL và nhận về thông tin chi tiết để hiển thị hoặc chia sẻ ảnh. Ảnh được lưu trong thư mục api/up/anh/
.
Base URL: https://upanh.nappay.vn/api.php
1. Upload Ảnh từ File
Upload một hoặc nhiều ảnh (tối đa 10 file, mỗi file 5MB).
POST api.phpContent-Type: multipart/form-data
Parameters:
- images[]: Mảng các file ảnh cần upload (bắt buộc)
2. Upload Ảnh từ URL
Tải ảnh từ một URL công khai (tối đa 5MB).
POST api.phpContent-Type: application/x-www-form-urlencoded
Parameters:
- imageUrl: URL của ảnh cần tải (bắt buộc)
Response thành công
{
"success": true,
"message": "Upload ảnh từ URL thành công",
"data": [
{
"name": "WJ3BZRE1.png",
"new_name": "687335b777ab8_2c617835a8337e7706e4fc4961cfe7c9.png",
"success": true,
"url": "https://upanh.nappay.vn/api/up/anh/687335b777ab8_2c617835a8337e7706e4fc4961cfe7c9.png",
"html_embed": "
",
"direct_link": "https://upanh.nappay.vn/api/up/anh/687335b777ab8_2c617835a8337e7706e4fc4961cfe7c9.png",
"size": 32797,
"type": "image/png",
"timestamp": 1752380855
}
]
}
Ví dụ sử dụng
1. JavaScript (Fetch API) - Upload File
async function uploadImage(file) {
const formData = new FormData();
formData.append('images[]', file);
try {
const response = await fetch('api.php', {
method: 'POST',
body: formData
});
const result = await response.json();
console.log(result);
if (result.success) {
console.log('Link trực tiếp:', result.data[0].direct_link);
console.log('HTML Embed:', result.data[0].html_embed);
console.log('Kích thước:', (result.data[0].size / 1024).toFixed(2) + ' KB');
}
} catch (error) {
console.error('Lỗi:', error);
}
}
2. JavaScript (Fetch API) - Upload từ URL
async function uploadImageFromUrl(url) {
try {
const response = await fetch('api.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: 'imageUrl=' + encodeURIComponent(url)
});
const result = await response.json();
console.log(result);
if (result.success) {
console.log('Link trực tiếp:', result.data[0].direct_link);
console.log('HTML Embed:', result.data[0].html_embed);
console.log('Kích thước:', (result.data[0].size / 1024).toFixed(2) + ' KB');
}
} catch (error) {
console.error('Lỗi:', error);
}
}
// Ví dụ gọi hàm
uploadImageFromUrl('https://dichvulight.vn/upload/theme/WJ3BZRE1.png');
3. PHP (cURL) - Upload File
$ch = curl_init();
$file = new CURLFile('path/to/image.jpg');
curl_setopt($ch, CURLOPT_URL, 'api.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ['images[]' => $file]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$result = json_decode($response, true);
if ($result['success']) {
echo 'Link trực tiếp: ' . $result['data'][0]['direct_link'] . "\n";
echo 'HTML Embed: ' . $result['data'][0]['html_embed'] . "\n";
echo 'Kích thước: ' . ($result['data'][0]['size'] / 1024) . ' KB';
}
curl_close($ch);
4. PHP (cURL) - Upload từ URL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'api.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'imageUrl=' . urlencode('https://dichvulight.vn/upload/theme/WJ3BZRE1.png'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$result = json_decode($response, true);
if ($result['success']) {
echo 'Link trực tiếp: ' . $result['data'][0]['direct_link'] . "\n";
echo 'HTML Embed: ' . $result['data'][0]['html_embed'] . "\n";
echo 'Kích thước: ' . ($result['data'][0]['size'] / 1024) . ' KB';
}
curl_close($ch);
Các trường trong Response
Trường | Kiểu | Mô tả |
---|---|---|
success | Boolean | Trạng thái request (true/false) |
message | String | Thông báo kết quả |
data | Array | Mảng kết quả các file đã upload |
data[].name | String | Tên file gốc (hoặc tên từ URL) |
data[].new_name | String | Tên file mới trên server |
data[].url | String | URL đầy đủ đến ảnh |
data[].html_embed | String | Mã HTML để nhúng ảnh |
data[].direct_link | String | Link trực tiếp đến ảnh |
data[].size | Number | Kích thước file (bytes) |
data[].type | String | Loại file (MIME type, ví dụ: image/png) |
data[].timestamp | Number | Thời gian upload (Unix timestamp) |
Giới hạn
- Chỉ chấp nhận file ảnh (JPG, JPEG, PNG, GIF, WEBP)
- Kích thước tối đa mỗi file/URL: 5MB
- Tối đa 10 file mỗi lần upload (đối với upload file)
- Tổng dung lượng tối đa mỗi lần upload file: 50MB
- URL phải là liên kết trực tiếp đến ảnh công khai
Quay lại trang chủ
| © 2025 By Nappay.vn