Obtenir la hauteur de la largeur de l'image à distance de l'url

ainsi l'alerte donne des valeurs non définies pour la largeur et la hauteur. Je pense que les valeurs w et h de l'image de l'img.le calcul de la charge n'est pas passé aux valeurs à retourner, ou il peut revenir w et h avant la charge les calcule:

function getMeta(url){
 var w; var h;
 var img=new Image;
 img.src=url;
 img.onload=function(){w=this.width; h=this.height;};
 return {w:w,h:h}    
}

// "http://snook.ca/files/mootools_83_snookca.png" //1024x678
// "http://shijitht.files.wordpress.com/2010/08/github.png" //128x128

var end = getMeta("http://shijitht.files.wordpress.com/2010/08/github.png");
var w = end.w;
var h = end.h;
alert(w+'width'+h+'height');

Comment puis-je faire afficher la bonne largeur et la bonne hauteur sur l'alerte?

http://jsfiddle.net/YtqXk /

50
demandé sur Roko C. Buljan 2012-07-12 02:51:48

4 réponses

obtenir la taille de l'image avec jQuery

function getMeta(url){
    $("<img/>",{
        load : function(){
            alert(this.width+' '+this.height);
        },
        src  : url
    });
}

obtenir la taille d'image avec JavaScript

function getMeta(url){   
    var img = new Image();
    img.onload = function(){
        alert( this.width+' '+ this.height );
    };
    img.src = url;
}

obtenir la taille d'image avec JavaScript (navigateurs modernes, IE9+)

function getMeta(url){   
    var img = new Image();
    img.addEventListener("load", function(){
        alert( this.naturalWidth +' '+ this.naturalHeight );
    });
    img.src = url;
}

utilisez ce qui précède simplement comme: getMeta( "http://example.com/img.jpg" );

https://developer.mozilla.org/en/docs/Web/API/HTMLImageElement

84
répondu Roko C. Buljan 2017-06-24 12:45:19

il suffit de passer un appel comme argument comme ceci:

function getMeta(url, callback) {
    var img = new Image();
    img.src = url;
    img.onload = function() { callback(this.width, this.height); }
}
getMeta(
  "http://snook.ca/files/mootools_83_snookca.png",
  function(width, height) { alert(width + 'px ' + height + 'px') }
);
17
répondu feychou 2017-09-25 12:06:02

les variables w et h de la fonction img.onload n'ont pas la même portée que celles de la fonction getMeta() . Une façon de le faire est la suivante:

Fiddle : http://jsfiddle.net/ppanagi/28UES/2 /

function getMeta(varA, varB) {
    if (typeof varB !== 'undefined') {
       alert(varA + ' width ' + varB + ' height');
    } else {
       var img = new Image();
       img.src = varA;
       img.onload = getMeta(this.width, this.height);
    }
}


getMeta("http://snook.ca/files/mootools_83_snookca.png");
8
répondu Panagiotis Panagi 2013-07-24 20:53:48

ES6: en utilisant async/await vous pouvez le faire de manière séquentielle

async function getMeta(url) {
    return new Promise((resolve, reject) => {
        let img = new Image();
        img.onload = () => resolve(img);
        img.onerror = reject;
        img.src = url;
    });
}

et vous pouvez l'utiliser comme suit (qui est presque identique au code dans votre question (j'ajoute await mot-clé et change la variable end en img , et change var en let mot-clé)

let img = await getMeta("http://shijitht.files.wordpress.com/2010/08/github.png");
let w = img.width;
let h = img.height; 
alert(w+'width'+h+'height');

rappelez-vous que si vous appelez getMeta dans une certaine fonction il doit avoir async mot-clé dans sa définition.

2
répondu Kamil Kiełczewski 2018-06-27 14:51:25