Skip to content

Commit

Permalink
Merge pull request #513 from madscience/feature/rotate
Browse files Browse the repository at this point in the history
Add `ImageManipulator.rotate` method
  • Loading branch information
sgarner authored Jan 13, 2022
2 parents 89a8235 + 696a3ff commit 6e125be
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/ImageManipulator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 7 additions & 1 deletion src/ImageManipulator.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 6e125be

Please sign in to comment.