* Resizes the image to a set width and height using a 2-pass bilinear algorithm * @example * ```ts * import { Jimp } from "jimp"; * * const image = await Jimp.read("test/image.png"); * * image.resize({ w: 150 }); * ```
(image: I, options: ResizeOptions)
| 78 | * ``` |
| 79 | */ |
| 80 | resize<I extends JimpClass>(image: I, options: ResizeOptions) { |
| 81 | const { mode } = ResizeOptionsSchema.parse(options); |
| 82 | |
| 83 | let w: number; |
| 84 | let h: number; |
| 85 | |
| 86 | if (typeof options.w === "number") { |
| 87 | w = options.w; |
| 88 | h = options.h ?? image.bitmap.height * (w / image.bitmap.width); |
| 89 | } else if (typeof options.h === "number") { |
| 90 | h = options.h; |
| 91 | w = options.w ?? image.bitmap.width * (h / image.bitmap.height); |
| 92 | } else { |
| 93 | throw new Error("w must be a number"); |
| 94 | } |
| 95 | |
| 96 | // round inputs |
| 97 | w = Math.round(w) || 1; |
| 98 | h = Math.round(h) || 1; |
| 99 | |
| 100 | if (mode && typeof Resize2[mode] === "function") { |
| 101 | const dst = { |
| 102 | data: Buffer.alloc(w * h * 4), |
| 103 | width: w, |
| 104 | height: h, |
| 105 | }; |
| 106 | Resize2[mode](image.bitmap, dst); |
| 107 | image.bitmap = dst; |
| 108 | } else { |
| 109 | const resize = new (Resize as unknown as Constructable<ResizeClass>)( |
| 110 | image.bitmap.width, |
| 111 | image.bitmap.height, |
| 112 | w, |
| 113 | h, |
| 114 | true, |
| 115 | true, |
| 116 | (buffer: Buffer) => { |
| 117 | image.bitmap.data = Buffer.from(buffer); |
| 118 | image.bitmap.width = w; |
| 119 | image.bitmap.height = h; |
| 120 | } |
| 121 | ); |
| 122 | |
| 123 | resize.resize(image.bitmap.data); |
| 124 | } |
| 125 | |
| 126 | return image; |
| 127 | }, |
| 128 | |
| 129 | /** |
| 130 | * Uniformly scales the image by a factor. |
nothing calls this directly
no test coverage detected
searching dependent graphs…