Expose width and height in the file metadata

Fixes #149
pull/163/head
Romain 5 years ago
parent 282713f273
commit 5571e0e6bf

@ -30,6 +30,9 @@ class Metadata {
this.animated = animated(exiftool)
this.rating = rating(exiftool)
this.favourite = favourite(picasa)
const size = dimensions(exiftool)
this.width = size.width
this.height = size.height
this.exif = opts ? (opts.embedExif ? exiftool.EXIF : undefined) : undefined
// metadata could also include fields like
// - lat = 51.5
@ -127,4 +130,21 @@ function makeArray (value) {
return Array.isArray(value) ? value : [value]
}
function dimensions (exif) {
// Use the Composite field to avoid having to check all possible tag groups (EXIF, QuickTime, ASF...)
if (!exif.Composite) {
return {
width: null,
height: null
}
} else {
const size = exif.Composite.ImageSize
const x = size.indexOf('x')
return {
width: parseInt(size.substr(0, x), 10),
height: parseInt(size.substr(x + 1), 10)
}
}
}
module.exports = Metadata

@ -218,4 +218,21 @@ describe('Metadata', function () {
should(meta.favourite).eql(true)
})
})
describe('size', function () {
it('can get an image width and height', function () {
const exiftool = fixtures.exiftool()
exiftool.Composite = { ImageSize: '800x600' }
const meta = new Metadata(exiftool)
should(meta.width).eql(800)
should(meta.height).eql(600)
})
it('defaults to null otherwise', function () {
const exiftool = fixtures.exiftool()
const meta = new Metadata(exiftool)
should(meta.width).eql(null)
should(meta.width).eql(null)
})
})
})

Loading…
Cancel
Save