热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

从外部图像URL创建Blob-CreatingaBlobfromanexternalimageurl

Ihaveanfileinputthatiusedtogetafileandturnitintoablob.IsthereanywayIcangetan

I have an file input that i used to get a file and turn it into a blob. Is there anyway I can get an external image url and turn that into a blob? Here is the code I am using to do it with just a file from a :

我有一个文件输入,我用来获取文件并将其转换为blob。无论如何,我可以获得一个外部图像网址并将其转换为blob?以下是我用来处理中的文件的代码:

//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 

Instead of passing a file through I wanted to be able to structure this code to pass a image url and convert it into a blob.

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

1 个解决方案

#1


2  

I hope it helps you. (I didn't run it.)

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

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;
}

推荐阅读
author-avatar
月亮哥哥是团宠他男人
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有