From 752405020a50ef9039f276322793f4a1accfe8c8 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Wed, 12 Sep 2018 21:03:35 +0200 Subject: [PATCH] exif tweaks --- gatsby-node.js | 42 +++++++++++++++---- src/components/atoms/Exif.jsx | 60 +++------------------------ src/components/atoms/Exif.module.scss | 23 ++++++++-- src/components/atoms/Image.jsx | 2 +- 4 files changed, 61 insertions(+), 66 deletions(-) diff --git a/gatsby-node.js b/gatsby-node.js index 05f2b404..68308fd8 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -51,21 +51,49 @@ exports.onCreateNode = ({ node, actions, getNode }) => { // exif if (node.internal.mediaType === 'image/jpeg') { fastExif - .read(node.absolutePath) + .read(node.absolutePath, true) .then(exifData => { + if (!exifData) return generateExif(exifData, createNodeField, node) }) .catch(() => null) // just silently fail when exif can't be extracted } } +const getFraction = decimal => { + for (var denominator = 1; (decimal * denominator) % 1 !== 0; denominator++); + return { numerator: decimal * denominator, denominator: denominator } +} + const generateExif = (exifData, createNodeField, node) => { - const iso = exifData.exif.ISO || null - const model = exifData.image.Model || null - const fstop = exifData.exif.FNumber || null - const shutterspeed = exifData.exif.ExposureTime || null - const focalLength = exifData.exif.FocalLength || null - const exposure = exifData.exif.ExposureBiasValue || null + const { Model } = exifData.image + const { + ISO, + FNumber, + ExposureTime, + FocalLength, + ExposureBiasValue + } = exifData.exif + + const shutterspeedNumerator = getFraction(ExposureTime).numerator + const shutterspeedDenominator = getFraction(ExposureTime).denominator + const exposureShortened = parseFloat(ExposureBiasValue.toFixed(2)) + + const model = Model + const iso = `ISO ${ISO}` + const fstop = `ƒ ${FNumber}` + const shutterspeed = `${shutterspeedNumerator}/${shutterspeedDenominator}s` + const focalLength = `${FocalLength}mm` + + let exposure + + if (ExposureBiasValue === 0) { + exposure = `+/- ${exposureShortened} ev` + } else if (ExposureBiasValue > 0) { + exposure = `+ ${exposureShortened} ev` + } else { + exposure = `${exposureShortened} ev` + } // add exif fields to type File createNodeField({ diff --git a/src/components/atoms/Exif.jsx b/src/components/atoms/Exif.jsx index fe446d25..0efeed4c 100644 --- a/src/components/atoms/Exif.jsx +++ b/src/components/atoms/Exif.jsx @@ -2,65 +2,17 @@ import React from 'react' import PropTypes from 'prop-types' import styles from './Exif.module.scss' -const getFraction = decimal => { - for (var denominator = 1; (decimal * denominator) % 1 !== 0; denominator++); - return { numerator: decimal * denominator, denominator: denominator } -} - -const ExposureFormatted = ({ exposure }) => { - const exposureShortened = exposure.toFixed(0) - - if (exposureShortened === 0) { - return `+/- ${exposureShortened}` - } else if (exposureShortened > 0) { - return `+ ${exposureShortened}` - } else { - return exposureShortened - } -} - const Exif = ({ exif }) => { const { iso, model, fstop, shutterspeed, focalLength, exposure } = exif return ( ) } diff --git a/src/components/atoms/Exif.module.scss b/src/components/atoms/Exif.module.scss index 83a1b0b0..14255300 100644 --- a/src/components/atoms/Exif.module.scss +++ b/src/components/atoms/Exif.module.scss @@ -1,17 +1,32 @@ @import 'variables'; +@import 'mixins'; .exif { - font-size: $font-size-small; + font-size: $font-size-mini; color: $brand-grey-light; - margin-top: -($spacer); + margin-top: -($spacer * 1.5); margin-bottom: $spacer * 2; display: flex; flex-wrap: wrap; - justify-content: space-between; text-align: center; span { - flex: 1; + display: block; + flex: 0 0 33%; white-space: nowrap; + padding: $spacer / 4; + + &:first-child { + flex-basis: 100%; + } + } + + @media (min-width: $screen-sm) { + span { + &, + &:first-child { + flex: 1 1 auto; + } + } } } diff --git a/src/components/atoms/Image.jsx b/src/components/atoms/Image.jsx index e961ab57..80686bc1 100644 --- a/src/components/atoms/Image.jsx +++ b/src/components/atoms/Image.jsx @@ -8,7 +8,7 @@ const Image = ({ fluid, fixed, alt }) => (