UNPKG

multi-automator

Version:
49 lines (48 loc) 1.45 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.extract = void 0; /** * @desc: image operate * @author: john_chen * @date: 2025.03.15 */ const sharp_1 = __importDefault(require("sharp")); const config_1 = require("../config"); /** * 图像裁切 * * @param {Buffer} imageBuffer * @param {Object} bounds * @return {Promise{Buffer}} */ async function extract(imageBuffer, bounds) { let { x, y, width, height } = bounds; // 进 1 let ceilX = Math.ceil(x); let ceilY = Math.ceil(y); if (ceilX > x) { width = width - (ceilX - x); } if (ceilY > y) { height = height - (ceilY - y); } // 去掉小数部分 let floorWidth = Math.floor(width); let floorHeight = Math.floor(height); if (floorWidth < 1 || floorHeight < 1) { throw new Error(`The height and width must be at least 1, received: height=${height} width=${width}`); } config_1.logger.info(`[image.extract] Bounds: ${JSON.stringify({ x: ceilX, y: ceilY, width: floorWidth, height: floorHeight })}`); let res = await (0, sharp_1.default)(imageBuffer).extract({ left: ceilX, top: ceilY, width: floorWidth, height: floorHeight }).png().toBuffer(); return res; } exports.extract = extract; ;