mirror of https://gitgud.io/fatchan/gm
add 'orientation' to getters, and autoOrient convenience method for fixing rotation from EXIF metadata
parent
1539a40e13
commit
0ac5bcbf65
7 changed files with 71 additions and 2 deletions
After Width: | Height: | Size: 9.9 KiB |
@ -0,0 +1,48 @@ |
||||
|
||||
// gm - Copyright Aaron Heckmann <aaron.heckmann+github@gmail.com> (MIT Licensed)
|
||||
|
||||
/** |
||||
* Extend proto. |
||||
*/ |
||||
|
||||
module.exports = function (proto) { |
||||
|
||||
var exifTransforms = { |
||||
topleft: '' |
||||
, topright: ['-flip', 'horizontal'] |
||||
, bottomright: ['-rotate', 180] |
||||
, bottomleft: ['-flip', 'vertical' ] |
||||
, lefttop: ['-transpose'] |
||||
, righttop: ['-rotate', 90] |
||||
, rightbottom: ['-transverse'] |
||||
, leftbottom: ['-rotate', 270] |
||||
} |
||||
|
||||
proto.autoOrient = function autoOrient () { |
||||
var self = this; |
||||
self.preProcess(function(callback) { |
||||
self.orientation(function (err, orientation) { |
||||
if (err) return callback(err); |
||||
|
||||
var transforms = exifTransforms[orientation.toLowerCase()]; |
||||
if(transforms) { |
||||
|
||||
// remove any existing transforms that might conflict
|
||||
var index = self._out.indexOf(transforms[0]); |
||||
if (~index) { |
||||
self._out.splice(index, transforms.length); |
||||
} |
||||
|
||||
self.out.apply(self, transforms); |
||||
|
||||
// strip EXIF profile since we can't reset Orientation=1
|
||||
self.noProfile(); |
||||
} |
||||
|
||||
callback(self); |
||||
}); |
||||
}); |
||||
|
||||
return self; |
||||
} |
||||
} |
@ -0,0 +1,13 @@ |
||||
|
||||
// gm - Copyright Aaron Heckmann <aaron.heckmann+github@gmail.com> (MIT Licensed)
|
||||
|
||||
module.exports = function (_, dir, finish, gm) { |
||||
|
||||
// this image is sideways, but may be auto-oriented by modern OS's
|
||||
// try opening it in a browser to see its true orientation
|
||||
gm(dir + '/originalSideways.jpg') |
||||
.autoOrient() |
||||
.write(dir + '/autoOrient.jpg', function autoOrient (err) { |
||||
finish(err); |
||||
}); |
||||
} |
Loading…
Reference in new issue