Files
Desktop/js/modules/autoOrientImage.js
Daniel Gasienica 4431854923 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
2018-02-15 15:14:59 -05:00

28 lines
738 B
JavaScript

// 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);
});
};