1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-11-15 09:35:21 +01:00

fix failing exif extraction for some photos

This commit is contained in:
Matthias Kretschmann 2019-10-04 16:31:20 +02:00
parent df1438e6b3
commit cc15aa149b
Signed by: m
GPG Key ID: 606EEEF3C479A91F
2 changed files with 55 additions and 38 deletions

View File

@ -2,40 +2,22 @@ const fastExif = require('fast-exif')
const Fraction = require('fraction.js') const Fraction = require('fraction.js')
const dms2dec = require('dms2dec') const dms2dec = require('dms2dec')
exports.createExifFields = (node, createNodeField) => { exports.createExifFields = async (node, createNodeField) => {
fastExif let exifData
.read(node.absolutePath, true) try {
.then(exifData => { exifData = await fastExif.read(node.absolutePath, true)
if (!exifData) return if (!exifData) return
constructExifFields(exifData, createNodeField, node) constructExifFields(exifData, createNodeField, node)
}) } catch (error) {
.catch(() => null) // just silently fail when exif can't be extracted // console.error(`${node.name}: ${error.message}`)
return null // just silently fail when exif can't be extracted
}
} }
const constructExifFields = (exifData, createNodeField, node) => { const getGps = gpsData => {
const { Model } = exifData.image if (!gpsData) return
const {
ISO,
FNumber,
ExposureTime,
FocalLength,
ExposureBiasValue
} = exifData.exif
const {
GPSLatitudeRef,
GPSLatitude,
GPSLongitudeRef,
GPSLongitude
} = exifData.gps
const { n, d } = new Fraction(ExposureTime) const { GPSLatitudeRef, GPSLatitude, GPSLongitudeRef, GPSLongitude } = gpsData
const exposureShortened = parseFloat(ExposureBiasValue.toFixed(2))
const model = `${Model}`
const iso = `ISO ${ISO}`
const fstop = `ƒ ${FNumber}`
const shutterspeed = `${n}/${d}s`
const focalLength = `${FocalLength}mm`
const GPSdec = dms2dec( const GPSdec = dms2dec(
GPSLatitude, GPSLatitude,
@ -47,16 +29,52 @@ const constructExifFields = (exifData, createNodeField, node) => {
const latitude = GPSdec[0] const latitude = GPSdec[0]
const longitude = GPSdec[1] const longitude = GPSdec[1]
return { latitude, longitude }
}
const getExposure = exposureMode => {
const exposureShortened = parseFloat(exposureMode.toFixed(2))
let exposure let exposure
if (ExposureBiasValue === 0) { if (exposureMode === 0) {
exposure = `+/- ${exposureShortened} ev` exposure = `+/- ${exposureShortened} ev`
} else if (ExposureBiasValue > 0) { } else if (exposureMode > 0) {
exposure = `+ ${exposureShortened} ev` exposure = `+ ${exposureShortened} ev`
} else { } else {
exposure = `${exposureShortened} ev` exposure = `${exposureShortened} ev`
} }
return exposure
}
const constructExifFields = (exifData, createNodeField, node) => {
const { Model } = exifData.image
const {
ISO,
FNumber,
ExposureTime,
FocalLength,
ExposureBiasValue,
ExposureMode
} = exifData.exif
const model = `${Model}`
const iso = `ISO ${ISO}`
const fstop = `ƒ ${FNumber}`
const focalLength = `${FocalLength}mm`
// Shutter speed
const { n, d } = new Fraction(ExposureTime)
const shutterspeed = `${n}/${d}s`
// GPS
let latitude
let longitude
if (exifData.gps) ({ latitude, longitude } = getGps(exifData.gps))
// Exposure
const exposure = getExposure(ExposureBiasValue || ExposureMode)
// add exif fields to type File // add exif fields to type File
createNodeField({ createNodeField({
node, node,
@ -68,10 +86,7 @@ const constructExifFields = (exifData, createNodeField, node) => {
shutterspeed, shutterspeed,
focalLength, focalLength,
exposure, exposure,
gps: { gps: { latitude, longitude }
latitude,
longitude
}
} }
}) })
} }

View File

@ -28,7 +28,9 @@ export default function Exif({ exif }: { exif: ExifProps }) {
{iso && <span title="ISO">{iso}</span>} {iso && <span title="ISO">{iso}</span>}
{focalLength && <span title="Focal length">{focalLength}</span>} {focalLength && <span title="Focal length">{focalLength}</span>}
</div> </div>
<div className={styles.map}>{gps && <ExifMap gps={gps} />}</div> {gps.latitude && (
<div className={styles.map}>{gps && <ExifMap gps={gps} />}</div>
)}
</aside> </aside>
) )
} }