0

When I upload an image in my web app it shows sideways so I am trying to use this module to rotate an image based on the EXIF data.

My code looks like this:

 <template>
  <div :class="$style.form247">
    <Canvas :url="image" :value="value" @input="$emit('input', $event)" />
      <div :class="$style.file">
      Choose file
      <input :class="$style.input" type="file" @change="onFileUpload">
    </div>
  </div>
</template>


<script>

import Canvas from './Canvas'

const jo = require('jpeg-autorotate')
const options = {quality: 100}



export default {
  props: ['value'],
  components: {
    Canvas
  },
  data() {
    return {
      image: null
    }
  },
  methods: {
    onFileUpload(event) {
      const selectedFile = event.target.files[0]
      const reader = new FileReader()


        const myBuffer = jo.rotate(selectedFile, options)
        .then(({buffer, orientation, dimensions, quality}) => {
          console.log(`Orientation was ${orientation}`)
          console.log(`Dimensions after rotation: ${dimensions.width}x${dimensions.height}`)
          console.log(`Quality: ${quality}`)
          // ...Do whatever you need with the resulting buffer...
          return buffer
        })
        .catch((error) => {
          console.log('An error occurred when rotating the file: ' + error.message)
        })

      reader.onload = (e) => this.image = e.target.result



      reader.readAsDataURL(selectedFile)

    }
  }
}
</script>

When I try to run my application I get an error saying that jo.rotate function doesn't have a path or buffer as a parameter, which is what I thought the selectedFile constant was. Basically I am confused as how to use this module within my code. I looked at the examples here in the sample usage section, but I am confused as how to how to use the rotate function. Like am I supposed to store the results of the jo.rotate function in a variable like this?

const myBuffer = jo.rotate(selectedFile, options)
.then(({buffer, orientation, dimensions, quality}) => {
  console.log(`Orientation was ${orientation}`)
  console.log(`Dimensions after rotation: ${dimensions.width}x${dimensions.height}`)
  console.log(`Quality: ${quality}`)
  // ...Do whatever you need with the resulting buffer...
  return buffer
})
.catch((error) => {
  console.log('An error occurred when rotating the file: ' + error.message)
})

and if so do I pass it to the readAsDataURL function like so reader.readAsDataURL(myBuffer)?

I am also pretty sure that where I currently have the jo.rotate function is wrong, but I am not sure about the correct place to put since I am new to javascript. I have the feeling that it needs to go in the reader.onload function, but I am not sure how to go about it.

alex
  • 11
  • 1
  • I hope jpeg-autorotate only runs in backend (Nodejs). If you need client side exif please go through following link https://stackoverflow.com/questions/20600800/js-client-side-exif-orientation-rotate-and-mirror-jpeg-images – Richardson. M Jan 03 '20 at 04:16
  • `jpeg-autorotate` doesn't work in the browser. See comment [here](https://github.com/johansatge/jpeg-autorotate/issues/6). – RobC Jan 03 '20 at 10:36
  • @RobC Thanks I didn't realize that it didn't work on the browser. – alex Jan 03 '20 at 19:24

1 Answers1

0

Unfortunately jpeg-autorotate doesn't work in browser. You will need to use exif parsing library to extract orientation and then rotate the image yourself with css transform:rotate(...) or canvas.

For the orientation parsing I would recommend exifr. It's fast at that and can handle hundreds of photos without crashing the browser or taking a long time :). Plus there's neat simple API and you feed it with pretty much anything - element, URL, Buffer, ArrayBuffer, even base64 url or string and more.

exifr.orientation(file).then(val => {
  console.log('orientation:', val)
})

This gives you an integer from 1 to 8 (learn more about these values here)

Mike Kovařík
  • 244
  • 2
  • 8