图片压缩API文档
概述
这是一个强大的图片压缩API,支持多种图片格式,可以显著减小图片文件大小,同时保持较好的图片质量。API支持以下特性:
- 支持多种图片格式(JPEG、PNG、WebP、BMP、TIFF)
- 可调节压缩质量
- 支持格式转换
- 返回Base64编码的图片数据
- 支持跨域请求
API端点
POST
https://compress.scdn.io/api/compress.php
请求参数
参数名 |
类型 |
必填 |
说明 |
image |
File |
是 |
要压缩的图片文件 |
quality |
Integer |
否 |
压缩质量(1-100),默认80 |
outputFormat |
String |
否 |
输出格式(auto, jpeg, png, webp),默认auto |
响应格式
{
"success": true,
"data": "Base64编码的图片数据",
"format": "image/webp",
"originalSize": 1024000,
"compressedSize": 204800,
"compressionRatio": 80
}
响应字段说明
字段名 |
类型 |
说明 |
success |
Boolean |
请求是否成功 |
data |
String |
Base64编码的压缩后图片数据 |
format |
String |
输出图片的MIME类型 |
originalSize |
Integer |
原始图片大小(字节) |
compressedSize |
Integer |
压缩后图片大小(字节) |
compressionRatio |
Integer |
压缩率(百分比) |
使用示例
JavaScript示例
// 使用FormData发送请求
const formData = new FormData();
formData.append('image', imageFile);
formData.append('quality', 80);
formData.append('outputFormat', 'webp');
const response = await fetch('https://compress.scdn.io/api/compress.php', {
method: 'POST',
body: formData
});
const result = await response.json();
// 使用压缩后的图片
const compressedImage = `data:${result.format};base64,${result.data}`;
PHP示例
// 使用cURL发送请求
$ch = curl_init('https://compress.scdn.io/api/compress.php');
$data = [
'image' => new CURLFile('path/to/image.jpg'),
'quality' => 80,
'outputFormat' => 'webp'
];
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$result = json_decode($response, true);
注意: 为了获得最佳的压缩效果,我们建议使用WebP格式,它通常能提供最好的压缩率。