@@ -98,13 +98,20 @@ type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
98
98
type Constructor < T > = new ( ...args : any [ ] ) => T ;
99
99
100
100
type JimpFormat <
101
- M extends string = string ,
102
- O extends Record < string , any > | undefined = undefined ,
103
- T extends Format < M , O > = Format < M , O > ,
101
+ MimeType extends string = string ,
102
+ EncodeOptions extends Record < string , any > | undefined = undefined ,
103
+ DecodeOptions extends Record < string , any > | undefined = undefined ,
104
+ T extends Format < MimeType , EncodeOptions , DecodeOptions > = Format <
105
+ MimeType ,
106
+ EncodeOptions ,
107
+ DecodeOptions
108
+ > ,
104
109
> = ( ) => T ;
105
110
106
111
type CreateMimeTypeToExportOptions < T extends Format < string , any > > =
107
112
T extends Format < infer M , infer O > ? Record < M , O > : never ;
113
+ type CreateMimeTypeToDecodeOptions < T extends Format < string , any > > =
114
+ T extends Format < infer M , any , infer O > ? Record < M , O > : never ;
108
115
type GetOptionsForMimeType < Mime extends string , MimeTypeMap > =
109
116
MimeTypeMap extends Record < Mime , infer O > ? O : never ;
110
117
@@ -138,6 +145,9 @@ export function createJimp<
138
145
type MimeTypeToExportOptions = CreateMimeTypeToExportOptions <
139
146
ReturnType < Formats [ number ] >
140
147
> ;
148
+ type MimeTypeToDecodeOptions = CreateMimeTypeToDecodeOptions <
149
+ ReturnType < Formats [ number ] >
150
+ > ;
141
151
type ExtensionToMimeType = CreateExtensionToMimeType < SupportedMimeTypes > ;
142
152
143
153
const plugins = pluginsArg || [ ] ;
@@ -213,7 +223,10 @@ export function createJimp<
213
223
* const image = await Jimp.read("https://upload.wikimedia.org/wikipedia/commons/0/01/Bot-Test.jpg");
214
224
* ```
215
225
*/
216
- static async read ( url : string | Buffer | ArrayBuffer ) {
226
+ static async read (
227
+ url : string | Buffer | ArrayBuffer ,
228
+ options ?: MimeTypeToDecodeOptions
229
+ ) {
217
230
if ( Buffer . isBuffer ( url ) || url instanceof ArrayBuffer ) {
218
231
return this . fromBuffer ( url ) ;
219
232
}
@@ -239,7 +252,7 @@ export function createJimp<
239
252
}
240
253
241
254
const buffer = bufferFromArrayBuffer ( data ) ;
242
- return this . fromBuffer ( buffer ) ;
255
+ return this . fromBuffer ( buffer , options ) ;
243
256
}
244
257
245
258
/**
@@ -314,7 +327,10 @@ export function createJimp<
314
327
* const image = await Jimp.fromBuffer(buffer);
315
328
* ```
316
329
*/
317
- static async fromBuffer ( buffer : Buffer | ArrayBuffer ) {
330
+ static async fromBuffer (
331
+ buffer : Buffer | ArrayBuffer ,
332
+ options ?: MimeTypeToDecodeOptions
333
+ ) {
318
334
const actualBuffer =
319
335
buffer instanceof ArrayBuffer ? bufferFromArrayBuffer ( buffer ) : buffer ;
320
336
@@ -331,7 +347,7 @@ export function createJimp<
331
347
}
332
348
333
349
const image = new CustomJimp (
334
- await format . decode ( actualBuffer )
350
+ await format . decode ( actualBuffer , options ?. [ format . mime ] )
335
351
) as InstanceType < typeof CustomJimp > & ExtraMethodMap ;
336
352
337
353
image . mime = mime . mime ;
0 commit comments