Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rotate JPEG image based on exif data #6

Open
vorg opened this issue Nov 5, 2016 · 2 comments
Open

Rotate JPEG image based on exif data #6

vorg opened this issue Nov 5, 2016 · 2 comments
Labels

Comments

@vorg
Copy link
Owner

vorg commented Nov 5, 2016

Portrait photos coming eg from iPhone are not rotated correctly.

@vorg vorg added the v2 label Nov 5, 2016
@vorg
Copy link
Owner Author

vorg commented Nov 5, 2016

Example solution using exif-js

var EXIF = require('exif-js')
var Image = window.Image

function rotateExifImage (imageObject, fileBlob, callback) {
  EXIF.getData(fileBlob, function () {
    var orientation = EXIF.getTag(this, 'Orientation')
    var rotation = 0
    var rotatedWidth = imageObject.width
    var rotatedHeight = imageObject.height

    switch (orientation) {
      case 2:
        // TODO
        // horizontal flip
        // context.translate(canvas.width, 0)
        // context.scale(-1, 1)
        break
      case 3:
        // 180° rotate left
        context.translate(canvas.width, canvas.height)
        context.rotate(Math.PI)
        break
      case 4:
        // TODO
        // vertical flip
        // context.translate(0, canvas.height)
        // context.scale(1, -1)
        break
      case 5:
        // TODO
        // vertical flip + 90 rotate right
        // context.rotate(0.5 * Math.PI)
        // context.scale(1, -1)
        break
      case 6:
        // 90° rotate right
        rotatedWidth = imageObject.height
        rotatedHeight = imageObject.width
        rotation = 0.5 * Math.PI
        break
      case 7:
        // TODO
        // horizontal flip + 90 rotate right
        // context.rotate(0.5 * Math.PI)
        // context.translate(canvas.width, -canvas.height)
        // context.scale(-1, 1)
        break
      case 8:
        // flip over
        context.translate(canvas.width, canvas.height)
        context.rotate(Math.PI)
        // rotate to the right 90 deg
        rotatedWidth = imageObject.height
        rotatedHeight = imageObject.width
        rotation = 0.5 * Math.PI
        break
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant