4
I am creating a system to organize the images of a site, that is, I will store information such as name, extension, size, height, width, ... and I am having difficulties to get the data of height and width
The codes currently are:
jQuery:
$(document).ready(function(){
$('#file').on('change',function(e){
var input = e.target;
var reader = new FileReader();
reader.onload = function(){
var dataURL = reader.result;
$('#img').prop('src',dataURL);
console.log($('#img'));
};
reader.readAsDataURL(input.files[0]);
var arquivo = input.files[0].name;
var split = arquivo.split('.');
var extensao = split[split.length-1];
var nome = arquivo.replace('.'+extensao,'');
var tamanho = (input.files[0].size/1024).toFixed(2);
var altura = 'ainda não descobri';
var largura = 'ainda não descobri';
$('#arquivo').html('Arquivo: '+arquivo);
$('#nome').html('Nome: '+nome);
$('#extensao').html('Extensão: '+extensao);
$('#tamanho').html('Tamanho: '+tamanho+' kB');
$('#altura').html('Altura: '+altura);
$('#largura').html('Largura: '+largura);
});
});
HTML:
<input id="file" type='file' accept='image/*'><br>
<br>
<p id="arquivo"></p>
<p id="nome"></p>
<p id="extensao"></p>
<p id="tamanho"></p>
<p id="altura"></p>
<p id="largura"></p>
<br>
<img id='img'>
I found that by writing console.log($('#img'));
onload causes the image information to appear on the console, but when typing $('#img').width();
or $('#img').height();
the result becomes 0. Someone can help me?
try the following var width = $('#img'). naturalWidth; var height = $('#img'). naturalHeight;
– Caique Romero
Gets 'Undefined' using the natural...
– Valter Junior
I believe it works only after loading the image
– Caique Romero
I got it with [0]. It got $('#img')[0]. naturalWidth; and $('#img')[0]. naturalHeight.
– Valter Junior
But it only takes from Monday on...
– Valter Junior