Skip to content

Commit

Permalink
feat(sharp): add composite, ensureAlpha & modulate
Browse files Browse the repository at this point in the history
  • Loading branch information
ikhsanalatsary committed May 3, 2019
1 parent f455f78 commit 830eed2
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 59 deletions.
12 changes: 4 additions & 8 deletions config/default.js
Expand Up @@ -3,13 +3,6 @@
module.exports = {
acl: 'private',
resize: true,
crop: false,
background: false,
embed: false,
max: false,
min: false,
withoutEnlargement: false,
ignoreAspectRatio: false,
extract: false,
trim: false,
flatten: false,
Expand All @@ -32,5 +25,8 @@ module.exports = {
toColorspace: false,
toFormat: false,
gzip: false,
metadata: {}
metadata: {},
composite: false,
ensureAlpha: false,
modulate: false
};
40 changes: 28 additions & 12 deletions index.d.ts
@@ -1,4 +1,18 @@
import { ResizeOptions, RGBA, Region, ExtendOptions, ThresholdOptions, AvailableFormatInfo, OutputOptions, JpegOptions, PngOptions, Metadata, Kernel } from 'sharp';
import {
ResizeOptions,
RGBA,
Region,
ExtendOptions,
ThresholdOptions,
AvailableFormatInfo,
OutputOptions,
JpegOptions,
PngOptions,
Kernel,
OverlayOptions,
FlattenOptions,
WriteableMetadata
} from 'sharp';
import { UploadOptions } from '@google-cloud/storage';

export declare interface Size {
Expand All @@ -18,6 +32,12 @@ export declare interface Threshold {
options?: ThresholdOptions;
}

export declare interface Modulate {
brightness?: number;
saturation?: number;
hue?: number;
}

export declare interface Format {
type: string | AvailableFormatInfo;
options?: OutputOptions | JpegOptions | PngOptions;
Expand All @@ -28,16 +48,10 @@ declare type SharpOption<T = string> = false | T;
export declare interface SharpOptions {
size?: Size;
resize?: boolean;
crop?: SharpOption<string | number>;
background?: SharpOption<RGBA | string>;
embed?: boolean;
max?: boolean;
min?: boolean;
withoutEnlargement?: boolean;
ignoreAspectRatio?: boolean;
composite?: Array<{ input: string | Buffer } & OverlayOptions>,
extract?: SharpOption<Region>;
trim?: SharpOption<number>;
flatten?: boolean;
flatten?: SharpOption<FlattenOptions>;
extend?: SharpOption<number | ExtendOptions>;
negate?: boolean;
rotate?: SharpOption<boolean | number>;
Expand All @@ -50,13 +64,14 @@ export declare interface SharpOptions {
greyscale?: boolean;
normalize?: boolean;
normalise?: boolean;
withMetadata?: SharpOption<Metadata>;
withMetadata?: SharpOption<WriteableMetadata>;
convolve?: SharpOption<Kernel>;
threshold?: SharpOption<number | Threshold>;
toColourspace?: SharpOption;
toColorspace?: SharpOption;
toFormat?: SharpOption<string | Format>;
gzip?: boolean;
ensureAlpha?: boolean;
modulate?: SharpOption<Modulate>;
}

declare interface Sizes extends Size {
Expand All @@ -69,7 +84,8 @@ export declare interface CloudStorageOptions extends UploadOptions {
keyFilename?: string;
filename?: string;
acl?: string;
sizes?: Sizes[]
sizes?: Sizes[];
gzip?: boolean;
}

export declare type MulterOptions = SharpOptions & CloudStorageOptions;
Expand Down
10 changes: 3 additions & 7 deletions lib/get-sharp-options.js
Expand Up @@ -2,11 +2,6 @@

module.exports = (options) => ({
resize: options.resize,
background: options.background,
crop: options.crop,
embed: options.embed,
max: options.max,
min: options.min,
toFormat: options.toFormat,
extract: options.extract,
trim: options.trim,
Expand All @@ -27,7 +22,8 @@ module.exports = (options) => ({
threshold: options.threshold,
toColourspace: options.toColourspace,
toColorspace: options.toColorspace,
ignoreAspectRatio: options.ignoreAspectRatio,
withMetadata: options.withMetadata,
withoutEnlargement: options.withoutEnlargement
composite: options.composite,
ensureAlpha: options.ensureAlpha,
modulate: options.modulate
});
2 changes: 0 additions & 2 deletions lib/transformer.js
Expand Up @@ -34,8 +34,6 @@ const validateValue = (value) => {
const resolveImageStream = (key, value, size, imageStream) => {
if (key === 'resize' && isObject(size)) {
imageStream = imageStream.resize(size.width, size.height, size.option);
} else if (key === 'crop') {
imageStream = imageStream[key](value);
} else if (key === 'toFormat') {
imageStream = imageStream.toFormat(validateFormat(value), value.options);
} else {
Expand Down
75 changes: 45 additions & 30 deletions test/implementation.test.js
Expand Up @@ -36,10 +36,10 @@ const storage = multerSharp({
height: 400,
option: {
kernel: 'lanczos2',
interpolator: 'nohalo'
interpolator: 'nohalo',
fit: 'inside'
}
},
max: true
}
});
const upload = multer({ storage });
const storage2 = multerSharp({
Expand All @@ -52,9 +52,11 @@ const storage2 = multerSharp({
acl: config.uploads.gcsUpload.acl,
size: {
width: 400,
height: 400
},
max: true
height: 400,
option: {
fit: 'inside'
}
}
});
const upload2 = multer({ storage: storage2 });

Expand All @@ -66,9 +68,11 @@ const storage3 = multerSharp({
destination: config.uploads.gcsUpload.destination,
size: {
width: 400,
height: 400
height: 400,
option: {
fit: 'inside'
}
},
max: true,
toColourspace: 'srgb'
});
const upload3 = multer({ storage: storage3 });
Expand All @@ -79,15 +83,19 @@ const storage4 = multerSharp({
keyFilename: config.uploads.gcsUpload.keyFilename,
acl: config.uploads.gcsUpload.acl,
destination: config.uploads.gcsUpload.destination,
size: { width: 200 },
crop: 16, // crop strategy
background: {
r: 0, g: 0, b: 100, alpha: 0
size: {
width: 200,
option: {
withoutEnlargement: true,
background: {
r: 0, g: 0, b: 100, alpha: 0
}
}
},
withoutEnlargement: true,
ignoreAspectRatio: true,
trim: 50,
flatten: true,
flatten: {
background: { r: 0, g: 0, b: 0 }
},
extend: {
top: 10, bottom: 20, left: 10, right: 10
},
Expand Down Expand Up @@ -116,21 +124,24 @@ const storage5 = multerSharp({
keyFilename: config.uploads.gcsUpload.keyFilename,
acl: config.uploads.gcsUpload.acl,
destination: config.uploads.gcsUpload.destination,
size: { width: 400, height: 400 },
crop: 'north',
background: {
r: 0, g: 0, b: 0, alpha: 0
size: {
width: 400,
height: 400,
option: {
position: 'north',
background: {
r: 0, g: 0, b: 0, alpha: 0
},
withoutEnlargement: true
}
},
embed: true,
max: true,
min: true,
withoutEnlargement: true,
ignoreAspectRatio: true,
extract: {
left: 0, top: 2, width: 50, height: 100
},
trim: 50,
flatten: true,
flatten: {
background: { r: 0, g: 0, b: 0 }
},
extend: {
top: 10, bottom: 20, left: 10, right: 10
},
Expand Down Expand Up @@ -164,9 +175,11 @@ const storage6 = multerSharp({
acl: config.uploads.gcsUpload.acl,
size: {
width: 400,
height: 400
height: 400,
option: {
fit: 'cover'
}
},
max: true,
extract: {
left: 0, top: 2, width: 400, height: 400
}
Expand Down Expand Up @@ -219,9 +232,11 @@ const storage12 = multerSharp({
destination: 'uploadArray',
size: {
width: 400,
height: 400
},
max: true
height: 400,
option: {
fit: 'fill'
}
}
});
const upload12 = multer({ storage: storage12 });

Expand Down

0 comments on commit 830eed2

Please sign in to comment.