add 'orientation' to getters, and autoOrient convenience method for fixing rotation from EXIF metadata

master
Evan Owen 13 years ago
parent 1539a40e13
commit 0ac5bcbf65
  1. 1
      .gitignore
  2. 2
      README.md
  3. BIN
      examples/imgs/originalSideways.jpg
  4. 1
      lib/convenience.js
  5. 48
      lib/convenience/autoOrient.js
  6. 8
      lib/getters.js
  7. 13
      test/autoOrient.js

1
.gitignore vendored

@ -3,6 +3,7 @@ examples/imgs/*
!examples/imgs/original.jpg
!examples/imgs/original.png
!examples/imgs/original.gif
!examples/imgs/originalSideways.jpg
!examples/imgs/morpher.jpg
*.swp
*.swo

@ -142,6 +142,7 @@ or clone the repo:
- getters
- [size](http://aheckmann.github.com/gm/#getters) - returns the size (WxH) of the image
- orientation - returns the EXIF orientation of the image
- [format](http://aheckmann.github.com/gm/#getters) - returns the image format (gif, jpeg, png, etc)
- [depth](http://aheckmann.github.com/gm/#getters) - returns the image color depth
- [color](http://aheckmann.github.com/gm/#getters) - returns the number of colors
@ -151,6 +152,7 @@ or clone the repo:
- manipulation
- [antialias](http://aheckmann.github.com/gm/#antialias)
- autoOrient
- [bitdepth](http://aheckmann.github.com/gm/#bitdepth)
- [blur](http://aheckmann.github.com/gm/#blur)
- [charcoal](http://aheckmann.github.com/gm/#charcoal)

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.9 KiB

@ -9,4 +9,5 @@ module.exports = function (proto) {
require("./convenience/thumb")(proto);
require("./convenience/morph")(proto);
require("./convenience/sepia")(proto);
require("./convenience/autoOrient")(proto);
}

@ -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;
}
}

@ -7,7 +7,7 @@
module.exports = function (proto) {
;['size', 'format', 'depth', 'color', 'res', 'filesize'].forEach(function (getter) {
;['size', 'orientation', 'format', 'depth', 'color', 'res', 'filesize'].forEach(function (getter) {
proto[getter] = function (opts, callback) {
if (!callback) callback = opts, opts = {};
@ -54,7 +54,7 @@ proto.identify = function identify (opts, callback) {
var args = ['identify', '-ping', '-verbose', self.sourceStream ? '-' : self.source];
self._exec("gm", args, function (err, stdout, stderr) {
self._exec("gm", args, function (err, stdout, stderr, cmd) {
if (err) {
return callback.call(self, err, stdout, stderr, cmd);
}
@ -79,6 +79,10 @@ proto.identify = function identify (opts, callback) {
}
};
handle.Orientation = function Orientation (val) {
data.orientation = val;
};
handle.Format = function Format (val) {
data.format = val.split(" ")[0];
};

@ -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…
Cancel
Save