Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View vdavid's full-sized avatar
🏠
Working from home

David Veszelovszki vdavid

🏠
Working from home
View GitHub Profile
@vdavid
vdavid / OrientationFixer.mjs
Last active August 15, 2023 17:45
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.
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;
};