使用示例
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}`;
将GIF动图压缩为WebP动图
// 同样使用FormData发送请求
const formData = new FormData();
formData.append('image', gifFile); // gifFile 是一个GIF动图文件
formData.append('quality', 75);
formData.append('outputFormat', 'webp_animated');
const response = await fetch('https://compress.scdn.io/api/compress.php', {
method: 'POST',
body: formData
});
const result = await response.json();
// result.format 将会是 "image/webp"
const animatedWebpImage = `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);