From 44318549235af01fd061c25f557c93fd21cebb7a Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Fri, 9 Feb 2018 16:53:23 -0500 Subject: [PATCH] Add `autoOrientImage` module This module exposes `loadImage` with a `Promise` based interface and pre- populates `orientation: true` option to auto-orient input. Returns data URL as string. The module uses a named export as refactoring references of modules with `default` (`module.exports`) export references can be error-prone. See: https://basarat.gitbooks.io/typescript/docs/tips/defaultIsBad.html --- js/modules/autoOrientImage.js | 27 +++++++++++++++++++++++++++ preload.js | 2 ++ 2 files changed, 29 insertions(+) create mode 100644 js/modules/autoOrientImage.js diff --git a/js/modules/autoOrientImage.js b/js/modules/autoOrientImage.js new file mode 100644 index 000000000..91ece18b8 --- /dev/null +++ b/js/modules/autoOrientImage.js @@ -0,0 +1,27 @@ +// File | Blob | URLString -> LoadImageOptions -> Promise +// +// Documentation for `options`: +// https://github.com/blueimp/JavaScript-Load-Image/tree/v2.18.0#options +exports.autoOrientImage = (fileOrBlobOrURL, options) => { + const optionsWithAutoOrient = Object.assign( + {}, + options, { + canvas: true, + orientation: true, + } + ); + + return new Promise((resolve, reject) => { + window.loadImage(fileOrBlobOrURL, canvasOrError => { + if (canvasOrError.type === 'error') { + const error = canvasOrError; + reject(error); + return; + } + + const canvas = canvasOrError; + const dataURL = canvas.toDataURL(); + resolve(dataURL); + }, optionsWithAutoOrient); + }); +}; diff --git a/preload.js b/preload.js index 49ee73ed3..035672871 100644 --- a/preload.js +++ b/preload.js @@ -71,6 +71,8 @@ window.nodeNotifier = require('node-notifier'); window.loadImage = require('blueimp-load-image'); + const {autoOrientImage} = require('./js/modules/autoOrientImage'); + window.autoOrientImage = autoOrientImage; // We pull this in last, because the native module involved appears to be sensitive to // /tmp mounted as noexec on Linux.