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

Handle Exif orientations for JPEG images (fixes #1670) #2296

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

prewett-toptal
Copy link

@prewett-toptal prewett-toptal commented Oct 19, 2023

Implements simple Exif parsing for JPEG files, enough to determine if there is an Exif tag, and if that tag has an orientation, rotates the image. This makes node-canvas compatible with browsers, which also rotate the image based on the Exif orientation tag. This fixes #1670.

A simple test program for 2 of the 3 paths follows. This loads the JPEG, draws it to the canvas, and saves the result to ./test.png. Test images are available (among other places) from https://github.com/noell/jpg-exif-test-images:

`
const fs = require('fs')
const { createCanvas, loadImage } = require('canvas')

// One source of sample images with EXIF rotations is at
// https://github.com/noell/jpg-exif-test-images

const imagePath = process.argv[2];
loadImage(imagePath).then(image => {
const w = image.naturalWidth
const h = image.naturalHeight
const canvas = createCanvas(w, h/, 'PDF'/) // uncomment to test the mime device path
const context = canvas.getContext('2d')
context.fillStyle = '#f0f';
context.fillRect(0, 0, w, h);
context.drawImage(image, 0, 0, w, h)

const buffer = canvas.toBuffer('image/png')
fs.writeFileSync('./test.png', buffer)

}).catch(e => {
console.log("[error]", e);
})
`

Since one major use of node-canvas is in the fabric project, which has not updated to the newly release 3.0 version yet, I think it would be helpful to backport to 2.11 (they use 2.11.2 at the moment), since it seems that 3.0 makes breaking changes for fabric. I initially made these changes on a branch from the v2.11.2 tag, and they merged with no problems, so I expect this changeset can be merged into a 2.11 branch. I can also create a separate pull request to whatever branch would be appropriate, if that is helpful.

  • Have you updated CHANGELOG.md?

@prewett-toptal
Copy link
Author

Would it be possible to get a yes/no on this? Thanks!

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

Successfully merging this pull request may close these issues.

EXIF Orientation metadata option does not affect when drawing image on canvas.
2 participants