From 696a3ff89e43eba2efe527d5a37168f594a43769 Mon Sep 17 00:00:00 2001 From: Simon Garner Date: Fri, 14 Jan 2022 07:22:51 +1300 Subject: [PATCH] Add ImageManipulator.rotate method --- src/ImageManipulator.spec.ts | 11 +++++++++++ src/ImageManipulator.ts | 8 +++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/ImageManipulator.spec.ts b/src/ImageManipulator.spec.ts index 5ca530a5..cb87cfbf 100644 --- a/src/ImageManipulator.spec.ts +++ b/src/ImageManipulator.spec.ts @@ -24,6 +24,17 @@ test('can resize an image', async () => { expect(metadata.height).toBe(400); }); +test('can rotate an image', async () => { + const manipulator = new ImageManipulator(fs.readFileSync(path.join(__dirname, 'test-image.png'))); + const rotatedImage = await manipulator.rotate(90).toBuffer(); + const metadata = await new ImageManipulator(rotatedImage).readMetadata(); + + expect(metadata.mimeType).toBe('image/png'); + expect(metadata.format).toBe('png'); + expect(metadata.width).toBe(800); + expect(metadata.height).toBe(1280); +}); + test('can apply blur effect to an image', async () => { const manipulator = new ImageManipulator(fs.readFileSync(path.join(__dirname, 'test-image.png'))); const blurredImage = await manipulator.blur(20).toBuffer(); diff --git a/src/ImageManipulator.ts b/src/ImageManipulator.ts index 61a6c422..f11e82c0 100644 --- a/src/ImageManipulator.ts +++ b/src/ImageManipulator.ts @@ -1,5 +1,5 @@ import { Duplex, Readable } from 'stream'; -import sharp, { JpegOptions, Metadata, PngOptions, Sharp, WebpOptions } from 'sharp'; +import sharp, { JpegOptions, Metadata, PngOptions, RotateOptions, Sharp, WebpOptions } from 'sharp'; import { ImageMetadata } from './ImageMetadata'; import { ImageDimensions } from './ImageDimensions'; import { ImageMimeType } from './ImageMimeType'; @@ -103,6 +103,12 @@ export class ImageManipulator { return this; } + public rotate(angle?: number, options?: RotateOptions): this { + this.image.rotate(angle, options); + + return this; + } + public blur(sigma: number | null): this { this.image.blur(sigma != null ? sigma : undefined);