![](/img/trans.png)
[英]I am using image-size module of Nodejs in my code .Can anyone tell me what unit it measures the dimensions of the image( its not pixels I checked )
[英]Retrieving image dimensions using image-size, and adding these values to an object
我是Node JS的新手,所以我可能完全錯了!
我創建了一個上傳表單來上傳圖片。 我有這個工作,圖像被上傳到公用文件夾中的文件夾。
我的下一步是獲取圖像的寬度和高度。 我使用了圖像大小的模塊,並設法獲得寬度和高度。
我想為我創建的對象添加寬度和高度,但是我似乎無法弄清楚如何將這些值添加到對象中。 然后,該對象用於將有關圖像的數據添加到數據庫。
uploadImages(req, res, (err) => {
if (err) {
console.log("Error:", err.message)
} else {
const imagesArray = req.files.map((image) => {
let imageObject = {}
imageObject.type = image.mimeType
imageObject.path = image.path
imageObject.link = image.path.replace("public", "").replace(/\\/g, "/")
imageObject.timeUploaded = Date.now()
sizeOf("public/" + imageObject.link, (err, sizes) => {
if(err) {
error(err)
} else {
success(sizes)
}
})
const error = (err) => {
console.log(err)
}
const success = (data) => {
imageObject.width = data.width
imageObject.height = data.height
console.log(imageObject)
}
return imageObject
})
db.collection("uploadtest2").insertMany(imagesArray, (err, result) => {
if (err) {
return console.log(err)
}
// db.collection("resized-images").insertMany()
console.log("something saved to the database")
})
}
})
res.redirect("/")
})```
嘗試是小提琴
https://jsfiddle.net/dupinderdhiman/wy7omr6k/5/
var img = document.getElementById('img'); //or however you get a handle to the IMG var width = img.clientWidth; var height = img.clientHeight; alert('height: '+height+' Width:'+width);
<img id="img" src="https://media.licdn.com/dms/image/C4D0BAQHiNSL4Or29cg/company-logo_200_200/0?e=2159024400&v=beta&t=0e00tehBFFtuqgUCfAijpOkoBl89jxOTIe_k9HHpi_4" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.