实现图片(png、gif、jpeg、wbmp、webp、xbm)格式随意来回转换
接口地址: http://api.ilingku.com/int/v1/image2
返回格式: json/image
请求方式: get/post
请求示例: http://api.ilingku.com/int/v1/image2?type=webp&format=down&url=https://y.gtimg.cn/music/photo_new/T002R300x300M000002Eubkz2ckWyC_1.jpg
请求参数说明:
名称 | 必填 | 类型 | 说明 |
---|---|---|---|
url | 是 | string | 需要转换的图片url |
type | 否 | string | 输出图片格式(png、gif、jpeg、wbmp、webp、xbm) |
format | 否 | string | 输出格式(down=直接下载,html=img标签、json=json格式)默认为显示图片 |
返回参数说明:
名称 | 类型 | 说明 |
---|---|---|
code | string | 返回状态码 |
msg | string | 返回错误提示 |
data | string | 返回转换好的 base64编码图片 |
返回示例:
{
"code": 200,
"msg": "转换成功",
"data": "data:image/webp;base64,qd65sa48hj86e62"
}
错误码格式说明:
名称 | 类型 | 说明 |
---|---|---|
code | string | 错误代码 |
msg | string | 错误提示 |
代码示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图片格式随意转换</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<form id="file-upload-form" enctype="multipart/form-data">
<select id="type" name="type">
<option value="png">png</option>
<option value="gif">gif</option>
<option value="jpeg">jpg</option>
</select>
<input type="file" id="file-input" name="file">
<button type="submit">转换格式</button>
</form>
<div id="lktext"><div>
<script>
$(document).ready(function() {
$('#file-upload-form').submit(function(e) {
e.preventDefault();
var formData = new FormData($(this)[0]);
$.ajax({
url: 'http://api.ilingku.com//int/v1/image2?format=json',
type: 'POST',
data: formData,
contentType: false,
processData: false,
success: function(response) {
if(response.code=="200"){
$("#lktext").html('<img src="' + response.data + '" alt="鼠标右键-目标另存为">');
}else{
alert(response.msg);
}
},
error: function() {
alert('File upload and conversion error');
}
});
});
});
</script>
</body>
</html>