Skip to content

Instantly share code, notes, and snippets.

@ilyador
Last active September 3, 2019 16:38
Show Gist options
  • Save ilyador/1ef1210e192994b9a546f64477d0565e to your computer and use it in GitHub Desktop.
Save ilyador/1ef1210e192994b9a546f64477d0565e to your computer and use it in GitHub Desktop.
Rotating a picture and uploading it as a file with blueimp-load-image . Image to canvas to blob to file to s3.
import React, { useState } from 'react'
import uuid from 'uuid/v4'
import loadImage from 'blueimp-load-image'
const handleChange = event => { // on file input change
let _file = event.target.files[0]
if (!_file) return
let _fileExt = _file.name.split('.').pop()
let _fileName = uuid() + '.' + _fileExt
loadImage(_file, canvas => {
canvas.toBlob(blob => {
let _fileUrl = URL.createObjectURL(blob)
setFileUrl(_fileUrl) // React specific: set state for file preview.
let file = new File([blob], _fileName)
// upload the file with whatever lin you need
// I used S3 so I used the file var as the paylod:
uploadFileData({
file: file,
fileName: _fileName,
fileUrl: _fileUrl
})
}, 'image/jpeg', 1)
},{
orientation: true, // this rotates the file
maxWidth: 600
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment