Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

David Veszelovszki vdavid

🏠
Working from home
View GitHub Profile
@vdavid
vdavid / OrientationFixer.mjs
Last active Nov 22, 2020
ES6 class with zero dependencies that determines the orientation of a JPEG file, front end or back end. Returns raw orientation, but can also give a CSS transform string to use for <img style="...">. Can also convert the file to a base64 string to use as the <img src="...">. Check out the demo file below for an example on how to use it.
View OrientationFixer.mjs
export default class OrientationFixer {
/**
* @param {File} file
* @returns {Promise<int>}
*/
async determineOrientation(file) {
const data = await this._readFileToArrayBuffer(file);
const dataView = new DataView(data);
return this._isJpegFile(data, dataView) ? (this._getOrientationValueFromJpegData(dataView) || 1) : 1;
};