接入教程
1. 注册获取API密钥
2. 使用POST请求上传图片到指定端点
3. 在响应中获取图片链接
4. 将链接嵌入您的应用或网站中
5. 可选:设置图片的隐私和过期时间
使用Python上传图片到Imgbb
python
import requests
url = 'https://api.imgbb.com/1/upload'
params = {
'key': 'YOUR_API_KEY'
}
files = {
'image': open('image.jpg', 'rb')
}
response = requests.post(url, params=params, files=files)
print(response.json())
使用PHP上传图片到Imgbb
php
<?php
$url = 'https://api.imgbb.com/1/upload';
$apiKey = 'YOUR_API_KEY';
$imageFile = 'image.jpg';
$postData = [
'key' => $apiKey,
'image' => base64_encode(file_get_contents($imageFile))
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
使用JavaScript上传图片到Imgbb
javascript
const formData = new FormData();
formData.append('key', 'YOUR_API_KEY');
formData.append('image', document.querySelector('input[type="file"]').files[0]);
fetch('https://api.imgbb.com/1/upload', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
常见问题
如何获取Imgbb的API密钥?
访问Imgbb官网,注册账户后,在账户设置或API部分可以生成或查看您的API密钥。
Imgbb支持哪些图片格式?
Imgbb支持常见的图片格式,如JPG、PNG、GIF和BMP,具体限制可查看官方文档。
上传图片后如何设置隐私?
在上传API请求中,可以添加参数如'expiration'设置过期时间,或使用其他隐私选项,详情参考API文档。
Aitishiku.com