简体   繁体   English

JQuery 文件输入 - 读取图像宽度和高度

[英]JQuery File Input - Read image width & height

I'm trying to read (input type="file") image file's original width / height.我正在尝试读取(输入类型=“文件”)图像文件的原始宽度/高度。 My code gives me "undefined".我的代码给了我“未定义”。 I suppose because i'm not loading image to server or anywhere.我想是因为我没有将图像加载到服务器或任何地方。

Here is my code;这是我的代码;

<script>
    $( document ).ready(function() {
        
        $('#texture_modal').click(function () {
        
        var texture_name = $('#texture_name').val();
        var thumb_img = $('#thumb_img').val().replace(/^.*\\/, "");
        var big_img = $('#big_img').val().replace(/^.*\\/, "");
        var real_img = $('#real_img').val().replace(/^.*\\/, "");
    var img_size = document.getElementById("real_img").files[0].size / (1024*1024); // Get real_img size in MB
        var texture_size = img_size.toFixed(2); // get rid of decimals in real_img size MB  
        var texture_category = $('#texture_category').val();
        var texture_description = $('#texture_description').val();
        
        // THIS IS THE STUFF WHICH I WANT TO GET IMAGE WIDTH
        var texture_dim = document.getElementById("real_img").naturalWidth;
        console.log(texture_dim);
    
        }); //End click function
        
    });  //End document ready
</script>

And here is my input fields.这是我的输入字段。 I have multiple file inputs, whichs are for thumbnail image, big image and real image.我有多个文件输入,用于缩略图、大图像和真实图像。 I need real image width only, others will be upload to server.我只需要真实的图像宽度,其他的将上传到服务器。 Here is my input fields;这是我的输入字段;

<!-- this fields are inside a bootstrap modal -->
<div class="modal-body">
<div class="input-group input-group-sm">
  <div class="input-group-prepend">
    <span class="input-group-text"><small>Texture Name</small></span>
  </div>
  <input type="text" class="form-control" id="texture_name">
</div>    

<div class="form-group pt-1">
    <small>Thumbnail Img(200*200)</small>
    <input type="file" class="form-control form-control-sm" id="thumb_img">
    <small>Big Img(445*445)</small>
    <input type="file" class="form-control form-control-sm" id="big_img">
    
<!-- this one i want to take width without post or upload anywhere -->
<small>Real Img</small>
<input type="file" class="form-control form-control-sm" id="real_img">

<!-- taking categories with php function -->
    <small>Category</small>
    <select id="texture_category" class="form-control form-control-sm">
    <option selected disabled>----- Choose a Category -----</option>
 <?php foreach($texture_categories as $key){?>    
     <option><?php echo $key; ?></option>
<?php } ?>
    </select>
    
    <small>Description :</small>
     <textarea id="texture_description" class="form-control form-control-sm"></textarea>
  </div>
</div>

can you try to assign an Image and get it你能尝试分配一个图像并得到它吗

 var fileUpload=document.getElementById("photoInput"); function Test(){ var reader = new FileReader(); reader.readAsDataURL(fileUpload.files[0]); reader.onload = function (e) { var image = new Image(); image.src = e.target.result; image.onload = function () { var width = this.naturalWidth || this.width; var height = this.naturalHeight || this.height; console.log(height,width) } }; }
 <div class="photo"> <input type="file" name="photo" id="photoInput" onchange="Test(this)"/> </div>

in your example在你的例子中

 $( document ).ready(function() { $(".modal").modal() }); function Test(){ var fileUpload=document.getElementById("thumb_img"); var reader = new FileReader(); reader.readAsDataURL(fileUpload.files[0]); reader.onload = function (e) { var image = new Image(); image.src = e.target.result; image.onload = function () { var width = this.naturalWidth || this.width; var height = this.naturalHeight || this.height; console.log(height,width) } }; }
 <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script> <?-- this fields are inside a bootstrap modal --> <div class="modal" tabindex="-1" role="dialog"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-body"> <div class="input-group input-group-sm"> <div class="input-group-prepend"> <span class="input-group-text"><small>Texture Name</small></span> </div> <input type="text" class="form-control" id="texture_name"> </div> <div class="form-group pt-1"> <small>Thumbnail Img(200*200)</small> <input type="file" class="form-control form-control-sm"onchange="Test(this)" id="thumb_img"> <small>Big Img(445*445)</small> <input type="file" class="form-control form-control-sm" id="big_img"> <?-- this one i want to take width without post or upload anywhere --> <small>Real Img</small> <input type="file" class="form-control form-control-sm" id="real_img"> <?-- taking categories with php function --> <small>Category</small> <select id="texture_category" class="form-control form-control-sm"> <option selected disabled>----- Choose a Category -----</option> <;php foreach($texture_categories as $key){?> <option><?php echo $key? :></option> <?php } ?> </select> <small>Description :</small> <textarea id="texture_description" class="form-control form-control-sm"></textarea> </div> </div> </div> </div> </div>

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

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