Created
May 15, 2018 08:53
-
-
Save fxmontigny/4842cc1ddb6cc4915ba4198838839d0b to your computer and use it in GitHub Desktop.
Auto rotate image and retour base64
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
onRotateImage(file, checkError = true) { | |
const promise = new Promise((resolve, reject) => { | |
const reader = new FileReader(); | |
reader.onload = (e) => { | |
this.getOrientation(file, (orientation) => { | |
if (orientation > 0) { | |
const image = new Image(); | |
image.src = e.target['result']; | |
image.onload = () => { | |
// only jpeg | |
const canvas = document.createElement('canvas'), | |
context = canvas.getContext('2d'); | |
switch (orientation) { | |
case 8: | |
canvas.width = image.height; | |
canvas.height = image.width; | |
context.save(); | |
context.translate(canvas.width / 2, canvas.height / 2); | |
context.rotate(-90 * Math.PI / 180); | |
context.drawImage(image, -image.width / 2, -image.height / 2); | |
context.restore(); | |
break; | |
case 3: | |
canvas.width = image.width; | |
canvas.height = image.height; | |
context.save(); | |
context.translate(canvas.width / 2, canvas.height / 2); | |
context.rotate(180 * Math.PI / 180); | |
context.drawImage(image, -image.width / 2, -image.height / 2); | |
context.restore(); | |
break; | |
case 6: | |
canvas.width = image.height; | |
canvas.height = image.width; | |
context.save(); | |
context.translate(canvas.width / 2, canvas.height / 2); | |
context.rotate(90 * Math.PI / 180); | |
context.drawImage(image, -image.width / 2, -image.height / 2); | |
context.restore(); | |
break; | |
default: | |
canvas.width = image.width; | |
canvas.height = image.height; | |
context.drawImage(image, 0, 0); | |
break; | |
} | |
resolve(canvas.toDataURL()); | |
}; | |
} else { | |
resolve(e.target['result']); | |
} | |
}); | |
}; | |
const possibleImageType = ['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml']; | |
if (possibleImageType.indexOf(file.type) !== -1) { | |
reader.readAsDataURL(file); | |
} else if (checkError) { | |
reject('Please select an image file'); | |
} | |
}); | |
return promise; | |
} | |
getOrientation(file, callback) { | |
const reader = new FileReader(); | |
reader.onload = function (e) { | |
const view = new DataView(e.target['result']); | |
if (view.getUint16(0, false) !== 0xFFD8) { | |
return callback(-2); | |
} | |
const length = view.byteLength; | |
let offset = 2; | |
while (offset < length) { | |
const marker = view.getUint16(offset, false); | |
offset += 2; | |
if (marker === 0xFFE1) { | |
if (view.getUint32(offset += 2, false) !== 0x45786966) { | |
return callback(-1); | |
} | |
const little = view.getUint16(offset += 6, false) === 0x4949; | |
offset += view.getUint32(offset + 4, little); | |
const tags = view.getUint16(offset, little); | |
offset += 2; | |
for (let i = 0; i < tags; i++) { | |
if (view.getUint16(offset + (i * 12), little) === 0x0112) { | |
return callback(view.getUint16(offset + (i * 12) + 8, little)); | |
} | |
} | |
} else if ((marker && 0xFF00) !== 0xFF00) { | |
break; | |
} else { | |
offset += view.getUint16(offset, false); | |
} | |
} | |
return callback(-1); | |
}; | |
reader.readAsArrayBuffer(file.slice(0, 64 * 1024)); | |
} | |
// To use | |
this.onRotateImage(file) | |
.then(base64 => { | |
// ... | |
}) | |
.catch(err => { | |
// ... | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment