gpt4 book ai didi

javascript - 从外部图像 url 创建 Blob

转载 作者:搜寻专家 更新时间:2023-10-31 08:47:06 25 4
gpt4 key购买 nike

我有一个文件输入,用于获取文件并将其转换为 blob。无论如何我可以获得外部图像 url 并将其转换为 blob?这是我用来处理来自 <input type="file" /> 的文件的代码:

//Process the file and resize it.
function processfile(file) {

if (!(/image/i).test(file.type)) {
alert("File " + file.name + " is not an image.");
return false;
}

// read the files
var reader = new FileReader();
reader.readAsArrayBuffer(file);

reader.onload = function (event) {
// blob stuff
var blob = new Blob([event.target.result]); // create blob...
window.URL = window.URL || window.webkitURL;
var blobURL = window.URL.createObjectURL(blob); // and get it's URL

// helper Image object
var image = new Image();
image.src = blobURL;
image.onload = function () {

for (var x = 0; x < self.ThumbSizes.length; x++) {
// have to wait till it's loaded
var resized = resizeMe(image, self.ThumbSizes[x]); // send it to canvas
var resized_blob = dataURItoBlob(resized);
uploadFile(resized_blob, self.ThumbSizes[x].Name);
}
}
};

我不想传递文件,而是希望能够构造此代码以传递图像 url 并将其转换为 blob。

最佳答案

希望对你有帮助。 (我没有运行它。)

function processfile(imageURL) {
var image = new Image();
var onload = function () {
var canvas = document.createElement("canvas");
canvas.width =this.width;
canvas.height =this.height;

var ctx = canvas.getContext("2d");
ctx.drawImage(this, 0, 0);

canvas.toBlob(function(blob) {
// do stuff with blob
});
};

image.onload = onload;
image.src = imageURL;
}

关于javascript - 从外部图像 url 创建 Blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16408987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com