imagerot
Version:
A lightweight, cross-environment image library for applying unique effects via raw image buffers.
29 lines (28 loc) • 1.6 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.saveBuffer = void 0;
const promises_1 = __importDefault(require("fs/promises"));
const canvas_1 = require("canvas");
const saveBuffer = (_a, path_1, ...args_1) => __awaiter(void 0, [_a, path_1, ...args_1], void 0, function* ({ data, width, height }, path, config = null) {
config = config || {};
const canvas = (0, canvas_1.createCanvas)(width, height);
const ctx = canvas.getContext('2d');
const imageData = ctx.createImageData(width, height);
imageData.data.set(data);
ctx.putImageData(imageData, 0, 0);
const buffer = canvas.toBuffer(config.mime || 'image/png', config);
return promises_1.default.writeFile(path, buffer);
});
exports.saveBuffer = saveBuffer;