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

+40
View File
@@ -0,0 +1,40 @@
import { JimpClass } from "@jimp/types";
import { ResizeStrategy } from "@jimp/plugin-resize";
import { z } from "zod";
declare const CoverOptionsSchema: 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. ResizeStrategy.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 CoverOptions = z.infer<typeof CoverOptionsSchema>;
export declare const methods: {
/**
* Scale the image so the given width and height keeping the aspect ratio. Some parts of the image may be clipped.
* @example
* ```ts
* import { Jimp } from "jimp";
*
* const image = await Jimp.read("test/image.png");
*
* image.cover(150, 100);
* ```
*/
cover<I extends JimpClass>(image: I, options: CoverOptions): 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;AAExC,OAAO,EAAE,cAAc,EAA4B,MAAM,qBAAqB,CAAC;AAE/E,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,QAAA,MAAM,kBAAkB;IACtB,uCAAuC;;IAEvC,wCAAwC;;IAExC,sDAAsD;;IAEtD,oDAAoD;;;;;;;;;;;;EAEpD,CAAC;AAEH,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,CAAC;AAE9D,eAAO,MAAM,OAAO;IAClB;;;;;;;;;;OAUG;UACG,CAAC,SAAS,SAAS,SAAS,CAAC,WAAW,YAAY;CAyC3D,CAAC"}
+57
View File
@@ -0,0 +1,57 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.methods = void 0;
const core_1 = require("@jimp/core");
const plugin_resize_1 = require("@jimp/plugin-resize");
const plugin_crop_1 = require("@jimp/plugin-crop");
const zod_1 = require("zod");
const CoverOptionsSchema = zod_1.z.object({
/** the width to resize the image to */
w: zod_1.z.number(),
/** the height to resize the image to */
h: zod_1.z.number(),
/** A bitmask for horizontal and vertical alignment */
align: zod_1.z.number().optional(),
/** a scaling method (e.g. ResizeStrategy.BEZIER) */
mode: zod_1.z.nativeEnum(plugin_resize_1.ResizeStrategy).optional(),
});
exports.methods = {
/**
* Scale the image so the given width and height keeping the aspect ratio. Some parts of the image may be clipped.
* @example
* ```ts
* import { Jimp } from "jimp";
*
* const image = await Jimp.read("test/image.png");
*
* image.cover(150, 100);
* ```
*/
cover(image, options) {
const { w, h, align = core_1.HorizontalAlign.CENTER | core_1.VerticalAlign.MIDDLE, mode, } = CoverOptionsSchema.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
? w / image.bitmap.width
: h / image.bitmap.height;
image = plugin_resize_1.methods.scale(image, {
f,
mode,
});
image = plugin_crop_1.methods.crop(image, {
x: ((image.bitmap.width - w) / 2) * alignH,
y: ((image.bitmap.height - h) / 2) * alignV,
w,
h,
});
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,qCAA4D;AAC5D,uDAA+E;A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,GAAG,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM;YAC9C,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,KAAK;YACxB,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC;QAE9B,KAAK,GAAG,uBAAa,CAAC,KAAK,CAAC,KAAK,EAAE;YACjC,CAAC;YACD,IAAI;SACL,CAAC,CAAC;QACH,KAAK,GAAG,qBAAW,CAAC,IAAI,CAAC,KAAK,EAAE;YAC9B,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,MAAM;YAC1C,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,MAAM;YAC3C,CAAC;YACD,CAAC;SACF,CAAC,CAAC;QAEH,OAAO,KAAK,CAAC;IACf,CAAC;CACF,CAAC"}
+3
View File
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
+40
View File
@@ -0,0 +1,40 @@
import { JimpClass } from "@jimp/types";
import { ResizeStrategy } from "@jimp/plugin-resize";
import { z } from "zod";
declare const CoverOptionsSchema: 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. ResizeStrategy.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 CoverOptions = z.infer<typeof CoverOptionsSchema>;
export declare const methods: {
/**
* Scale the image so the given width and height keeping the aspect ratio. Some parts of the image may be clipped.
* @example
* ```ts
* import { Jimp } from "jimp";
*
* const image = await Jimp.read("test/image.png");
*
* image.cover(150, 100);
* ```
*/
cover<I extends JimpClass>(image: I, options: CoverOptions): 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;AAExC,OAAO,EAAE,cAAc,EAA4B,MAAM,qBAAqB,CAAC;AAE/E,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,QAAA,MAAM,kBAAkB;IACtB,uCAAuC;;IAEvC,wCAAwC;;IAExC,sDAAsD;;IAEtD,oDAAoD;;;;;;;;;;;;EAEpD,CAAC;AAEH,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,CAAC;AAE9D,eAAO,MAAM,OAAO;IAClB;;;;;;;;;;OAUG;UACG,CAAC,SAAS,SAAS,SAAS,CAAC,WAAW,YAAY;CAyC3D,CAAC"}
+54
View File
@@ -0,0 +1,54 @@
import { VerticalAlign, HorizontalAlign } from "@jimp/core";
import { ResizeStrategy, methods as resizeMethods } from "@jimp/plugin-resize";
import { methods as cropMethods } from "@jimp/plugin-crop";
import { z } from "zod";
const CoverOptionsSchema = 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. ResizeStrategy.BEZIER) */
mode: z.nativeEnum(ResizeStrategy).optional(),
});
export const methods = {
/**
* Scale the image so the given width and height keeping the aspect ratio. Some parts of the image may be clipped.
* @example
* ```ts
* import { Jimp } from "jimp";
*
* const image = await Jimp.read("test/image.png");
*
* image.cover(150, 100);
* ```
*/
cover(image, options) {
const { w, h, align = HorizontalAlign.CENTER | VerticalAlign.MIDDLE, mode, } = CoverOptionsSchema.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
? w / image.bitmap.width
: h / image.bitmap.height;
image = resizeMethods.scale(image, {
f,
mode,
});
image = cropMethods.crop(image, {
x: ((image.bitmap.width - w) / 2) * alignH,
y: ((image.bitmap.height - h) / 2) * alignV,
w,
h,
});
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,aAAa,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC5D,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,kBAAkB,GAAG,CAAC,CAAC,MAAM,CAAC;IAClC,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,oDAAoD;IACpD,IAAI,EAAE,CAAC,CAAC,UAAU,CAAC,cAAc,CAAC,CAAC,QAAQ,EAAE;CAC9C,CAAC,CAAC;AAIH,MAAM,CAAC,MAAM,OAAO,GAAG;IACrB;;;;;;;;;;OAUG;IACH,KAAK,CAAsB,KAAQ,EAAE,OAAqB;QACxD,MAAM,EACJ,CAAC,EACD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