检测是否是icon、png图片的icojs库

2021-01-21240次阅读javascript

用于检测是否是icon图片或者是png图片的icojs库。

检测是否是ICON图片

'use strict';

const toDataView = require('to-data-view');

/**
 * Check the ArrayBuffer is valid ICO.
 *
 * @alias module:ICO
 * @param {ArrayBuffer|Buffer} source - ICO file data.
 * @returns {boolean} True if arg is ICO.
 */
const isICO = source => {
  const dataView = toDataView(source);
  return dataView.getUint16(0, true) === 0 && dataView.getUint16(2, true) === 1;
};

module.exports = isICO;

检测是否是png图片

'use strict';

const toDataView = require('to-data-view');

/**
 * Check the ArrayBuffer is valid PNG.
 *
 * @access private
 * @param {ArrayBuffer|Buffer} source - ArrayBuffer or Buffer object.
 * @returns {boolean} Arg is PNG or not.
 */
const isPNG = source => {
  const dataView = toDataView(source);
  return dataView.getUint32(0, false) === 0x89504E47 && dataView.getUint32(4, false) === 0x0D0A1A0A;
};

module.exports = isPNG;

https://github.com/egy186/icojs

上一篇: fetch请求结果中文乱码解决方案  下一篇: js字符串转DOM,DOM转字符串伪代码  

检测是否是icon、png图片的icojs库相关文章