![](/img/trans.png)
[英]How to get in JavaScript width and height of image, before it's loaded?
[英]Javascript get the image width, height and video's resolution
這是我的代碼:
if($('#profileimg').val())
{
var fsize = $('#profileimg')[0].files[0].size; //get file size
var ftype = $('#profileimg')[0].files[0].type; // get file type
if(fsize>5242880)
{
$("#filetype").html("<b> Profile Image "+bytesToSize(fsize) +"</b> <br />File is too big, it should be less than 5 MB.");
return false
}
if(filetypeimage(ftype))
{
var file, img;
if ((file = $('#profileimg')[0])) {
img = new Image();
img.onload = function() {
alert(this.width + " " + this.height);
};
img.onerror = function() {
alert( "not a valid file: " + file.type);
};
img.src = _URL.createObjectURL(file);
}
我想獲取圖像的寬度和高度,也想獲取將要上傳到我們服務器上的視頻的分辨率。
提前致謝
window.URL = window.URL || window.webkitURL; $('#submit').on('click',function(e){ e.preventDefault(); var fileInput = $('#file')[0], file = fileInput.files && fileInput.files[0]; if( file ) { var img = new Image(); img.src = window.URL.createObjectURL( file ); img.onload = function() { var width = img.naturalWidth, height = img.naturalHeight; window.URL.revokeObjectURL( img.src ); alert(width +" "+ height); }; } }); // you can validate on file change also $("#file").change(function(e) { var file, img; if ((file = this.files[0])) { img = new Image(); console.log(this); img.onload = function() { alert(this.width + " " + this.height); }; img.onerror = function() { alert( "not a valid file: " + file.type); }; img.src = window.URL.createObjectURL(file); } });
<!DOCTYPE html> <html> <body> <form action="#" method="post"> <input type="file" id="file" /> <input type="submit" id="submit" /> </form> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> </body> </html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.