Javascript – Rotate uploaded pictures containing EXIF orientation in Parse Cloud Code

exifjavascriptnode.jsparse-platform

Portrait pictures, taken from some mobile devices, uploaded via an HTML form gets the wrong orientation while embedded in a web page.

This is due to the EXIF orientation metadata, which could for instance have the value 6 = Rotate 90 CW telling the image to be displayed with a specific orientation. However, the image itself – without metadata – is stored sideways as a horizontal image. Depending on the image renderer, you will either see the image correctly (as the left thumbnail below) or without the rotation metadata applied (as the right thumbnail). For images embedded in websites, it is the latter.

Portrait picture with EXIF orientation Rotate 90 CW, displayed with and without metadata applied

Is there any way to rotate the uploaded picture manually using either Javascript or Node.js, in a Parse Cloud Code hosted web app? (Parse Cloud Code only supports a few dependencies – but you could still upload small scripts yourself).

Best Answer

You should be able to use npm modules in cloud code: Using npm modules in cloud code

Once you've got that working, the jpegorientation npm module should meet your requirements:

var jpeg = require('jpegorientation');

jpeg.autoRotate('image.jpg', function (err) {
    // error handler 
});

If you can't get the npm modules working, you can always include the library manually. If you run into problems with that library and node-gyp, there are other modules to consider: