autoOrient.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * Extend proto.
  3. */
  4. module.exports = function (proto) {
  5. var exifTransforms = {
  6. topleft: ''
  7. , topright: ['-flop']
  8. , bottomright: ['-rotate', 180]
  9. , bottomleft: ['-flip']
  10. , lefttop: ['-flip', '-rotate', 90]
  11. , righttop: ['-rotate', 90]
  12. , rightbottom: ['-flop', '-rotate', 90]
  13. , leftbottom: ['-rotate', 270]
  14. }
  15. proto.autoOrient = function autoOrient () {
  16. // Always strip EXIF data since we can't
  17. // change/edit it.
  18. // imagemagick has a native -auto-orient option
  19. // so does graphicsmagick, but in 1.3.18.
  20. // apt doesn't release this version yet.
  21. if (this._options.imageMagick) {
  22. this.out('-auto-orient');
  23. this.strip();
  24. return this;
  25. }
  26. this.preprocessor(function (callback) {
  27. this.orientation({bufferStream: true}, function (err, orientation) {
  28. if (err) return callback(err);
  29. var transforms = exifTransforms[orientation.toLowerCase()];
  30. if (transforms) {
  31. // remove any existing transforms that might conflict
  32. var index = this._out.indexOf(transforms[0]);
  33. if (~index) {
  34. this._out.splice(index, transforms.length);
  35. }
  36. // repage to fix coordinates
  37. this._out.unshift.apply(this._out, transforms.concat('-page', '+0+0'));
  38. }
  39. this.strip();
  40. callback();
  41. });
  42. });
  43. return this;
  44. }
  45. }