Initial commit

This commit is contained in:
robert committed 2025-05-30 18:13:30 +08:00
commit e49726988c
5445 files changed
+1089914

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
import { JimpClass } from "@jimp/types";
import { ResizeStrategy } from "@jimp/plugin-resize";
import { z } from "zod";
declare const ContainOptionsSchema: z.ZodObject<{
/** the width to resize the image to */
w: z.ZodNumber;
/** the height to resize the image to */
h: z.ZodNumber;
/** A bitmask for horizontal and vertical alignment */
align: z.ZodOptional<z.ZodNumber>;
/** a scaling method (e.g. Jimp.RESIZE_BEZIER) */
mode: z.ZodOptional<z.ZodNativeEnum<typeof ResizeStrategy>>;
}, "strip", z.ZodTypeAny, {
w: number;
h: number;
align?: number | undefined;
mode?: ResizeStrategy | undefined;
}, {
w: number;
h: number;
align?: number | undefined;
mode?: ResizeStrategy | undefined;
}>;
export type ContainOptions = z.infer<typeof ContainOptionsSchema>;
export declare const methods: {
/**
* Scale the image to the given width and height keeping the aspect ratio. Some parts of the image may be letter boxed.
* @param w the width to resize the image to
* @param h the height to resize the image to
* @param align A bitmask for horizontal and vertical alignment
* @param mode a scaling method (e.g. Jimp.RESIZE_BEZIER)
* @example
* ```ts
* import { Jimp } from "jimp";
*
* const image = await Jimp.read("test/image.png");
*
* image.contain({ w: 150, h: 100 });
* ```
*/
contain<I extends JimpClass>(image: I, options: ContainOptions): I;
};
export {};
//# sourceMappingURL=index.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAGxC,OAAO,EAAE,cAAc,EAA4B,MAAM,qBAAqB,CAAC;AAE/E,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,QAAA,MAAM,oBAAoB;IACxB,uCAAuC;;IAEvC,wCAAwC;;IAExC,sDAAsD;;IAEtD,iDAAiD;;;;;;;;;;;;EAEjD,CAAC;AAEH,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,oBAAoB,CAAC,CAAC;AAElE,eAAO,MAAM,OAAO;IAClB;;;;;;;;;;;;;;OAcG;YACK,CAAC,SAAS,SAAS,SAAS,CAAC,WAAW,cAAc;CA6C/D,CAAC"}
+59
View File
@@ -0,0 +1,59 @@
import { HorizontalAlign, VerticalAlign } from "@jimp/core";
import { clone } from "@jimp/utils";
import { ResizeStrategy, methods as resizeMethods } from "@jimp/plugin-resize";
import { methods as blitMethods } from "@jimp/plugin-blit";
import { z } from "zod";
const ContainOptionsSchema = z.object({
/** the width to resize the image to */
w: z.number(),
/** the height to resize the image to */
h: z.number(),
/** A bitmask for horizontal and vertical alignment */
align: z.number().optional(),
/** a scaling method (e.g. Jimp.RESIZE_BEZIER) */
mode: z.nativeEnum(ResizeStrategy).optional(),
});
export const methods = {
/**
* Scale the image to the given width and height keeping the aspect ratio. Some parts of the image may be letter boxed.
* @param w the width to resize the image to
* @param h the height to resize the image to
* @param align A bitmask for horizontal and vertical alignment
* @param mode a scaling method (e.g. Jimp.RESIZE_BEZIER)
* @example
* ```ts
* import { Jimp } from "jimp";
*
* const image = await Jimp.read("test/image.png");
*
* image.contain({ w: 150, h: 100 });
* ```
*/
contain(image, options) {
const { w, h, align = HorizontalAlign.CENTER | VerticalAlign.MIDDLE, mode, } = ContainOptionsSchema.parse(options);
const hbits = align & ((1 << 3) - 1);
const vbits = align >> 3;
// check if more flags than one is in the bit sets
if (!((hbits !== 0 && !(hbits & (hbits - 1))) ||
(vbits !== 0 && !(vbits & (vbits - 1))))) {
throw new Error("only use one flag per alignment direction");
}
const alignH = hbits >> 1; // 0, 1, 2
const alignV = vbits >> 1; // 0, 1, 2
const f = w / h > image.bitmap.width / image.bitmap.height
? h / image.bitmap.height
: w / image.bitmap.width;
const c = resizeMethods.scale(clone(image), { f, mode });
image = resizeMethods.resize(image, { w, h, mode });
image.scan((_, __, idx) => {
image.bitmap.data.writeUInt32BE(image.background, idx);
});
image = blitMethods.blit(image, {
src: c,
x: ((image.bitmap.width - c.bitmap.width) / 2) * alignH,
y: ((image.bitmap.height - c.bitmap.height) / 2) * alignV,
});
return image;
},
};
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC5D,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpC,OAAO,EAAE,cAAc,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAC/E,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC3D,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,oBAAoB,GAAG,CAAC,CAAC,MAAM,CAAC;IACpC,uCAAuC;IACvC,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE;IACb,wCAAwC;IACxC,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE;IACb,sDAAsD;IACtD,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAC5B,iDAAiD;IACjD,IAAI,EAAE,CAAC,CAAC,UAAU,CAAC,cAAc,CAAC,CAAC,QAAQ,EAAE;CAC9C,CAAC,CAAC;AAIH,MAAM,CAAC,MAAM,OAAO,GAAG;IACrB;;;;;;;;;;;;;;OAcG;IACH,OAAO,CAAsB,KAAQ,EAAE,OAAuB;QAC5D,MAAM,EACJ,CAAC,EACD,CAAC,EACD,KAAK,GAAG,eAAe,CAAC,MAAM,GAAG,aAAa,CAAC,MAAM,EACrD,IAAI,GACL,GAAG,oBAAoB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAExC,MAAM,KAAK,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QACrC,MAAM,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