UNPKG

peter-generator

Version:

A simple thumbnail generator for JS.

148 lines (135 loc) 5.7 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (factory((global['Peter-generator'] = {}))); }(this, (function (exports) { 'use strict'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; const Sharp = require('./constructor'); require('./input')(Sharp); require('./resize')(Sharp); require('./composite')(Sharp); require('./operation')(Sharp); require('./colour')(Sharp); require('./channel')(Sharp); require('./output')(Sharp); require('./utility')(Sharp); module.exports = Sharp; var _sharp = /*#__PURE__*/Object.freeze({ }); var sharp = _sharp; var defaultOpts = { maxAttempts: 5, maxWidth: 70, sizeLimit: '100kB', }; var ThumbnailGenerator = /** @class */ (function () { function ThumbnailGenerator(opts) { if (opts === void 0) { opts = {}; } try { this.checkPropsType(opts); } catch (e) { console.error(e); } var mergedOpts = __assign({}, defaultOpts, opts); this.maxAttempts = mergedOpts.maxAttempts; this.maxWidth = mergedOpts.maxWidth; this.sizeLimit = this.harmonizeByteSize(mergedOpts.sizeLimit); } ThumbnailGenerator.prototype.checkPropsType = function (opts) { if (opts.maxWidth && typeof opts.maxWidth !== 'number') { throw new Error('The maxWidth option should be of type number'); } if (opts.sizeLimit && typeof opts.sizeLimit !== 'string' && typeof opts.sizeLimit !== 'number') { throw new Error('The sizeLimit option should be of type string or number'); } if (opts.maxAttempts && typeof opts.maxAttempts !== 'number') { throw new Error('The maxAttempts option should be of type number'); } }; ThumbnailGenerator.prototype.harmonizeByteSize = function (size) { if (typeof size === 'number') { return Math.floor(size); } var match = size.trim().match(/(-?\d*(?:\.\d+)?)\s*(\D*)/); if (!match[1]) { throw new Error('The numerical value of limit is missing'); } if (match[2] && !match[2].match(/^(kB|MB|GB)$/i)) { throw new Error('The unit value of limit is not valid (expected: kB, MB, GB)'); } var value = parseInt(match[1], 10); return Math.floor(match[2].toLowerCase() === 'gb' ? value * 1073741824 : match[2].toLowerCase() === 'mb' ? value * 1048576 : match[2].toLowerCase() === 'kb' ? value * 1024 : value); }; ThumbnailGenerator.prototype.resize = function (img, opts) { var _this = this; img.metadata() .then(function (_a) { var height = _a.height, size = _a.size, width = _a.width; if (size <= _this.sizeLimit) { img.toBuffer() .then(opts.onSuccess) .catch(opts.onFail); } else if (opts.attemptN && opts.attemptN > _this.maxAttempts) { opts.onFail('Number of attempts exceeded, failed to generate thumbnail'); } else { var nextWidth_1 = opts.width || _this.maxWidth; return img.resize(Math.floor(nextWidth_1), Math.floor(nextWidth_1 / (opts.ratio || width / height))) .toBuffer() .then(function (data) { _this.resize(sharp(data), __assign({}, opts, { attemptN: opts.attemptN ? opts.attemptN + 1 : 1, width: nextWidth_1 / 1.5 })); }); } }) .catch(opts.onFail); }; ThumbnailGenerator.prototype.generate = function (input) { var _this = this; if (!Buffer.isBuffer(input)) { throw new Error('The generate method expects an input of type Buffer'); } var img = sharp(input); return new Promise(function (resolve, reject) { _this.resize(img, { onSuccess: resolve, onFail: reject, }); }); }; return ThumbnailGenerator; }()); exports.ThumbnailGenerator = ThumbnailGenerator; Object.defineProperty(exports, '__esModule', { value: true }); })));