Add support for arbitrary rotation angle via vips_rotate (#1385)

This commit is contained in:
freezy
2018-09-27 19:00:36 +02:00
committed by Lovell Fuller
parent 37d385fafa
commit 796738da65
8 changed files with 163 additions and 36 deletions

View File

@@ -108,6 +108,8 @@ const Sharp = function (input, options) {
embed: 0,
useExifOrientation: false,
angle: 0,
rotationAngle: 0,
rotationBackground: [0, 0, 0, 255],
rotateBeforePreExtract: false,
flip: false,
flop: false,

View File

@@ -1,14 +1,18 @@
'use strict';
const color = require('color');
const is = require('./is');
/**
* Rotate the output image by either an explicit angle
* or auto-orient based on the EXIF `Orientation` tag.
*
* If an angle is provided, it is converted to a valid 90/180/270deg rotation.
* If an angle is provided, it is converted to a valid positive degree rotation.
* For example, `-450` will produce a 270deg rotation.
*
* If an angle that is not a multiple of 90 is provided, the color of the
* background color can be provided with the `background` option.
*
* If no angle is provided, it is determined from the EXIF data.
* Mirroring is supported and may infer the use of a flip operation.
*
@@ -29,16 +33,29 @@ const is = require('./is');
* readableStream.pipe(pipeline);
*
* @param {Number} [angle=auto] angle of rotation, must be a multiple of 90.
* @param {Object} [options] - if present, is an Object with optional attributes.
* @param {String|Object} [options.background="#000000"] parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha.
* @returns {Sharp}
* @throws {Error} Invalid parameters
*/
function rotate (angle) {
function rotate (angle, options) {
if (!is.defined(angle)) {
this.options.useExifOrientation = true;
} else if (is.integer(angle) && !(angle % 90)) {
this.options.angle = angle;
} else if (is.number(angle)) {
this.options.rotationAngle = angle;
if (is.object(options) && options.background) {
const backgroundColour = color(options.background);
this.options.rotationBackground = [
backgroundColour.red(),
backgroundColour.green(),
backgroundColour.blue(),
Math.round(backgroundColour.alpha() * 255)
];
}
} else {
throw new Error('Unsupported angle: angle must be a positive/negative multiple of 90 ' + angle);
throw new Error('Unsupported angle: angle must be a number.');
}
return this;
}