กำหนดขนาดดั้งเดิมของเบราว์เซอร์ข้ามรูปภาพหรือไม่


90

มีวิธีที่เชื่อถือได้และเป็นอิสระจากกรอบในการกำหนดมิติทางกายภาพของการ<img src='xyz.jpg'>ปรับขนาดในฝั่งไคลเอ็นต์หรือไม่?


img.onload = function () {console.log(img.height, img.width)}
neaumusic

คำตอบ:


130

คุณมี 2 ตัวเลือก:

ตัวเลือกที่ 1:

ถอดwidthและheightคุณลักษณะและอ่านoffsetWidthและoffsetHeight

ทางเลือกที่ 2:

สร้างImageวัตถุJavaScript ตั้งค่าsrcและอ่านwidthและheight(คุณไม่จำเป็นต้องเพิ่มลงในหน้าเพื่อดำเนินการนี้)

function getImgSize(imgSrc) {
    var newImg = new Image();

    newImg.onload = function() {
      var height = newImg.height;
      var width = newImg.width;
      alert ('The image size is '+width+'*'+height);
    }

    newImg.src = imgSrc; // this must be done AFTER setting onload
}

แก้ไขโดย Pekka : ตามที่ตกลงไว้ในความคิดเห็นฉันเปลี่ยนฟังก์ชันให้ทำงานในเหตุการณ์ ononload´ ของรูปภาพ ไม่งั้นมีภาพใหญ่heightและwidthจะไม่คืนอะไรเลยเพราะยังไม่ได้โหลดภาพ


ซึ่งจะใช้ไม่ได้กับภาพที่ยังไม่โหลด มันอาจจะคุ้มค่าที่จะปรับมันให้ทำงานอย่างถูกต้องสำหรับคนอื่น ๆ ที่สะดุดกับคำตอบนี้
James

11
@ Gabriel ฉันใช้สิ่งนี้ แต่มีnewImg.onloadฟังก์ชั่นเพื่อให้แน่ใจว่าภาพถูกโหลดเมื่อฉันตั้งค่าความกว้าง / ความสูง คุณตกลงให้ฉันแก้ไขคำตอบของคุณตามนั้นหรือไม่
Pekka

2
หมายเหตุด้านข้าง: Chrome / OSX อาจมีปัญหากับภาพแคชโดยที่คุณได้ 0 เป็นความสูง / ความกว้างโดยใช้เทคนิคนี้
David Hellsing

2
ฉันจะกลับความสูงและความกว้างได้อย่างไร ... ?? เนื่องจากตัวแปรเหล่านั้นไม่ได้รับนอก onload
แจ็ค

5
@GabrielMcAdams หากคุณเพิ่มif(newImg.complete || newImg.readyState === 4) newImg.onload();ในตอนท้ายของฟังก์ชันของคุณมันจะแก้ไขปัญหาบน Chrome / OSX ที่ทำให้ onload ไม่ทำงานเมื่อโหลดรูปภาพจากแคช
Prestaul

102

รูปภาพ (อย่างน้อยบน Firefox) มีnaturalWidthคุณสมบัติa / height เพื่อให้คุณสามารถใช้img.naturalWidthเพื่อรับความกว้างดั้งเดิมได้

var img = document.getElementsByTagName("img")[0];
img.onload=function(){
    console.log("Width",img.naturalWidth);
    console.log("Height",img.naturalHeight);
}

ที่มา


4
หัวข้อนี้ยังคงมีความเกี่ยวข้องในปี 2013 นี่คือลิงก์ที่มีวิธีแก้ปัญหาที่ยอดเยี่ยมสำหรับ IE7 / 8: jacklmoore.com/notes/naturalwidth-and-naturalheight-in-ie
BurninLeo

4
นี่คือคำตอบที่ชนะในปี 2018 ใช้ได้ทุกที่ (ตามตารางความเข้ากันได้ของเบราว์เซอร์ที่ MDN)
7vujy0f0hy

3

คุณสามารถโหลดรูปภาพไว้ในวัตถุรูปภาพจาวาสคริปต์จากนั้นตรวจสอบคุณสมบัติความกว้างและความสูงของวัตถุนั้น


แน่นอน - ฉันไม่ต้องใส่ลงในเอกสาร จะทำแบบนั้นไชโย!
Pekka

3
/* Function to return the DOM object's in crossbrowser style */
function widthCrossBrowser(element) {
    /* element - DOM element */

    /* For FireFox & IE */
    if(     element.width != undefined && element.width != '' && element.width != 0){
        this.width  =   element.width;
    }
    /* For FireFox & IE */
    else if(element.clientWidth != undefined && element.clientWidth != '' && element.clientWidth != 0){
        this.width  =   element.clientWidth;
    }
    /* For Chrome * FireFox */
    else if(element.naturalWidth != undefined && element.naturalWidth != '' && element.naturalWidth != 0){
        this.width  =   element.naturalWidth;
    }
    /* For FireFox & IE */
    else if(element.offsetWidth != undefined && element.offsetWidth != '' && element.offsetWidth != 0){
        this.width  =   element.offsetWidth;
    }       
        /*
            console.info(' widthWidth width:',      element.width);
            console.info(' clntWidth clientWidth:', element.clientWidth);
            console.info(' natWidth naturalWidth:', element.naturalWidth);
            console.info(' offstWidth offsetWidth:',element.offsetWidth);       
            console.info(' parseInt(this.width):',parseInt(this.width));
        */
    return parseInt(this.width);

}   

var elementWidth    = widthCrossBrowser(element);

คือelementการเลือก jQuery? แล้วความสูงล่ะ?
Istiaque Ahmed

2

เพียงแค่เปลี่ยนตัวเลือกที่สองของ Gabriel เล็กน้อยเพื่อให้ใช้งานได้ง่ายขึ้น:

function getImgSize(imgSrc, callback) {
    var newImg = new Image();

    newImg.onload = function () {
        if (callback != undefined)
            callback({width: newImg.width, height: newImg.height})
    }

    newImg.src = imgSrc;
}

Html:

<img id="_temp_circlePic" src="http://localhost/myimage.png" 
style="width: 100%; height:100%">

โทรตัวอย่าง:

getImgSize($("#_temp_circlePic").attr("src"), function (imgSize) {
    // do what you want with the image's size.
    var ratio = imgSize.height / $("#_temp_circlePic").height();
});
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.