简体   繁体   English

如何在JavaScript中获取文件api图像大小?

[英]How to get file api image size in javascript?

I have: 我有:

if(window.FileReader) {
    reader = new FileReader();
    var sendingcontext = sendingcanvas.getContext('2d');
    reader.onload = (function(theFile) { 
    var image = new Image();
    image.src = theFile.target.result;

    image.onload = function() {
    sendingcanvas.width=this.width;
    sendingcanvas.height= this.height;
    console.log("height: " + sendingcanvas.height + " width: " + sendingcanvas.width)
         sendingcontext.drawImage(image, 0,0);
    };
        });
        reader.onloadend = function(e){
        showUploadedItem(e.target.result, file.fileName);
    }
    reader.readAsDataURL(file);
    alert(sendingcanvas.toDataURL());
}

I Suppose I don't have to explain what the code does, But what I want is to get the file api image height and width, set it to sendingcanvas and send it to server using ajax by using it's toDataURL() . 我想我不必解释代码的作用,但是我想要获得的是文件api图像的高度和宽度,将其设置为sendingcanvas ,然后使用toDataURL()使用ajax将其发送到服务器。 I know This is weird but If I remove the alert(sendingcanvas.toDataURL()) from my script My server receives an empty image but If I have it in my script In my server the image is exactly how the client have it. 我知道这很奇怪,但是如果我从脚本中删除了alert(sendingcanvas.toDataURL()) ,我的服务器会收到一个空图像,但是如果我的脚本中包含该图像,则该图像正是客户端所拥有的图像。

Thanks for everything. 谢谢你所做的一切。

You can try let the image in a <img> tag and get the real width and size of the image in case what you having a DOM: 您可以尝试将图像放在<img>标记中,以获取图像的实际宽度和大小,以防万一您拥有DOM:

 var height = $( "#main-reference" ).get( 0 ).naturalWidth; var width = $( "#main-reference" ).get( 0 ).naturalHeight; 

this property gives you the real height and width of the img although you have broken the img with css 该属性为您提供了img的实际高度和宽度,尽管您已经用css破坏了img

Just want to point out that the image you are uploading is being converted to a png (if that is what you want) 只是要指出您正在上传的图像已被转换为png(如果您要的是)
The uploaded file is no longer the same as the user selected 上传的文件不再与所选用户相同

Btw, sending a blob is better then uploading a base64 string that is ~3x larger you can use this polyfill to get support for canvas.toBlob(...) in older browsers 顺便说一句,送斑点是更好然后上传一个base64字符串,它是〜3倍大,你可以使用这个填充工具以获取支持canvas.toBlob(...)在旧的浏览器

This is basicly what you are doing: 基本上,这就是您正在执行的操作:

 function showUploadedItem(image){ document.body.appendChild(image) } // Simulate a file (image) var black_1x1 = new Uint8Array([71,73,70,56,57,97,1,0,1,0,128,0,0,5,4,4,0,0,0,44,0,0,0,0,1,0,1,0,0,2,2,68,1,0,59]) var file = new Blob([black_1x1], {type:"image/gif"}) console.log(file) var img = new Image img.onload = function() { console.log("height: " + this.height + " width: " + this.width) var canvas = document.createElement('canvas') var ctx = canvas.getContext('2d') canvas.width = this.width canvas.height = this.height showUploadedItem(this) ctx.drawImage(this, 0, 0) canvas.toBlob(function(blob){ console.log(blob) // Send the canvas pic to server (using fetch, xhr or jQuery // With or without FormData // fetch('/upload', {method: 'post', body: blob}) }) } // No need to use fileReader + base64 img.src = URL.createObjectURL(file) 
 /* Just to make the pixel more vissible */ body img { width: 10px } 
 <!-- toBlob polyfill --> <script src="https://cdn.rawgit.com/eligrey/canvas-toBlob.js/master/canvas-toBlob.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM