【发布时间】:2019-04-30 16:12:32
【问题描述】:
我正在尝试使用 ajax 在 imgur 服务器上上传 gif 图像。代码适用于 png 和 jpg。但是当我尝试附加 gif 图像时,它会给我 png 文件 URL 作为响应。下面是代码
image.onload = function() {
var MAX_WIDTH = 500;
var MAX_HEIGHT = 500;
var tempW = image.width;
var tempH = image.height;
if (tempW > tempH) {
if (tempW > MAX_WIDTH) {
tempH *= MAX_WIDTH / tempW;
tempW = MAX_WIDTH;
}
} else {
if (tempH > MAX_HEIGHT) {
tempW *= MAX_HEIGHT / tempH;
tempH = MAX_HEIGHT;
}
}
var canvas = document.createElement("canvas");
canvas.width = tempW;
canvas.height = tempH;
var ctx = canvas.getContext("2d");
ctx.drawImage(this, 0, 0, tempW, tempH);
dataURL = canvas.toDataURL();
dataURL = dataURL.replace(/^data:image\/(png|jpg|gif);base64,/, "");
$.ajax({
url: 'https://api.imgur.com/3/image',
type: 'post',
headers: {
Authorization: 'Client-ID *************'
},
data: {
image: dataURL
},
dataType: 'json',
error: function(response) {
console.log(response);
},
success: function(response) {
if (response.success) {
console.log(response.data.link);
$('#snippet_image').val(response.data.link);
}
}
});
}
我试过了,我已经添加了带有 png|jpg 的 gif,如下所示
dataURL = dataURL.replace(/^data:image\/(png|jpg|gif);base64,/, "");
【问题讨论】:
标签: javascript jquery ajax imgur