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
This commit is contained in:
Daniel Gasienica 2018-02-09 16:53:23 -05:00
parent c77063afc6
commit 4431854923
2 changed files with 29 additions and 0 deletions

View File

@ -0,0 +1,27 @@
// File | Blob | URLString -> LoadImageOptions -> Promise<DataURLString>
//
// 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);
});
};

View File

@ -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.