Skip to content

Instantly share code, notes, and snippets.

@mceachen
Created December 9, 2016 05:22
Show Gist options
  • Save mceachen/961c19c1973d617b4243f7c316acb692 to your computer and use it in GitHub Desktop.
Save mceachen/961c19c1973d617b4243f7c316acb692 to your computer and use it in GitHub Desktop.
// Type definitions for sharp v0.16.2
// Project: https://github.com/lovell/sharp
// Definitions by: Matthew McEachen <https://github.com/mceachen/>
/// <reference types="node" />
import * as stream from "stream"
import * as events from "events"
// See https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-function-d-ts.html
export = sharp
declare function sharp(
input?: Buffer | string,
options?: sharp.Options
): sharp.Sharp;
declare namespace sharp {
interface Sharp extends InfoEvents, Operations, Resize, Output, stream.Duplex {
// Overridden methods must have all decls in one place: https://github.com/Microsoft/TypeScript/issues/2871
on(event: "info", callback: (info: OutputInfo) => void): this
on(event: "close", listener: () => void): this;
on(event: "drain", listener: () => void): this;
on(event: "error", listener: (err: Error) => void): this;
on(event: "finish", listener: () => void): this;
on(event: "pipe", listener: (src: stream.Readable) => void): this;
on(event: "unpipe", listener: (src: stream.Readable) => void): this;
on(event: string, listener: Function): this;
}
interface Maximums {
width: number
height: number
pixels: number
}
const maximum: Maximums
interface QueueEvents {
on(event: 'change', callback: (queueLength: number) => void): void
}
const queue: QueueEvents
type FormatSupportFlags = {
buffer: boolean
file: boolean
stream: boolean
}
type FormatType = ('jpeg' | 'png' | 'webp' | 'tiff' | 'raw')
type Format = {
[formatName: string]: {
id: FormatType,
input: FormatSupportFlags,
output: FormatSupportFlags
}
}
const format: Format
const versions: {[libraryName: string]: string}
interface InfoEvents {
on(event: 'info', callback: (info: OutputInfo) => void): void
}
interface Operations {
rotate(angle: number): Sharp
extract(options: ExtractOptions): Sharp
flip(enable?: boolean): Sharp
flop(enable?: boolean): Sharp
sharpen(sigma?: number, flat?: number, jagged?: number): Sharp
blur(sigma?: number): Sharp
extend(extend: number | ExtendOptions): Sharp
flatten(enable?: boolean): Sharp
trim(tolerance?: number): Sharp
gamma(gamma?: number): Sharp
negate(enable?: boolean): Sharp
normalise(enable?: boolean): Sharp
normalize(enable?: boolean): Sharp
convolve(kernel: Kernel): Sharp
threshold(threshold?: number, options?: ThresholdOptions): Sharp
boolean(operand: Buffer | string, operator: BooleanOperator, options?: BooleanOptions): Sharp
}
interface Raw {
width: number
height: number
channels: number
}
interface Options {
density?: number
raw?: Raw
}
interface ExtractOptions {
top: number
left: number
width: number
height: number
}
interface ExtendOptions {
top: number
left: number
bottom: number
right: number
}
interface Kernel {
width: number
height: number
/** must have length === width * height */
kernel: number[]
scale?: number
offset?: number
}
interface ThresholdOptions {
greyscale?: boolean
grayscale?: boolean // colours are hard
}
type BooleanOperator = ("and" | "or" | "eor")
interface BooleanOptions {
raw: Raw
}
// Resize
enum gravity {
center,
centre,
north,
east,
south,
west,
northeast,
southeast,
southwest,
northwest
}
enum strategy {
entropy,
attention
}
type ResizeKernel = ("cubic" | "lanczos2" | "lanczos3")
type Interpolator = (
"nearest" | "bilinear" | "bicubic" | "nohalo" |
"lbb" | "locallyBoundedBicubic" |
"vsqbs" | "vertexSplitQuadraticBasisSpline"
)
type ResizeOptions = {
kernel?: ResizeKernel
interpolator?: Interpolator
centreSampling?: boolean
centerSampling?: boolean
}
interface Resize {
resize(width?: number, height?: number, options?: ResizeOptions): Sharp
crop(gravityOrStrategy?: gravity | strategy): Sharp
embed(): Sharp
max(): Sharp
min(): Sharp
ignoreAspectRatio(): Sharp
withoutEnlargement(enable?: boolean): Sharp
}
interface OutputInfo {
format: string
size: number
width: number
height: number
channels: number
}
interface WithMetadataOptions {
orientation?: (1 | 2 | 3 | 4 | 5 | 6 | 7 | 8)
}
interface LossyImageOptions {
/** value between 1 and 100 inclusive */
quality?: number
force?: boolean
}
interface JpegOptions extends LossyImageOptions {
progressive?: boolean
trellisQuantisation?: boolean
overshootDeringing?: boolean
optimiseScans?: boolean
optimizeScans?: boolean
}
interface PngOptions {
progressive?: boolean
/** zlib compression level. Default = 6. */
compressionLevel?: number
adaptiveFiltering?: number
force?: boolean
}
interface HasFormatType<T extends FormatType> {
id: T
}
interface TileOptions {
/** value between 1 and 8192. Default = 256. */
size?: number
/** value between 0 and 8192. Default = 0. */
overlap?: number
container?: ('fs' | 'zip')
layout?: ('dz' | 'zoomify' | 'google')
}
interface Output {
toFile(fileOut: string): Promise<OutputInfo>
toFile(fileOut: string, callback: (err: any, info: OutputInfo) => void): void
toBuffer(): Promise<Buffer>
toBuffer(callback: (err: any, buffer: Buffer, info: OutputInfo) => void): void
withMetadata(withMetadata?: boolean | WithMetadataOptions): Sharp
jpeg(options?: JpegOptions): Sharp
png(options?: PngOptions): Sharp
webp(options?: LossyImageOptions): Sharp
tiff(options?: LossyImageOptions): Sharp
raw(): Sharp,
toFormat(format: 'jpeg' | HasFormatType<'jpeg'>, options?: JpegOptions): Sharp
toFormat(format: 'png' | HasFormatType<'png'>, options?: PngOptions): Sharp
toFormat(format: 'webp' | HasFormatType<'webp'>, options?: LossyImageOptions): Sharp
toFormat(format: 'tiff' | HasFormatType<'tiff'>, options?: LossyImageOptions): Sharp
tile(options?: TileOptions): Sharp
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment