Skip to main content

读取文件大小

常用库

  • probe-image-size
  • images-size (一些从adobe api 生成的jpg报错)

probe-image-size

安装

yarn add -D probe-image-size @types/probe-image-size

基础使用

import probe from 'probe-image-size';

// Get by URL
// 通过URL读取文件大小
let result = await probe('http://example.com/image.jpg');
console.log(result); // =>
/*
{
width: xx,
height: yy,
type: 'jpg',
mime: 'image/jpeg',
wUnits: 'px',
hUnits: 'px',
url: 'http://example.com/image.jpg'
}
*/


// By URL with options
// 配置
let result = await probe('http://example.com/image.jpg', { rejectUnauthorized: false });
console.log(result);


// From the stream
// 通过文件流(异步)
let result = await probe(require('fs').createReadStream('image.jpg'));
console.log(result);


// From a Buffer (sync)
// 通过二进制(同步)
let data = require('fs').readFileSync('image.jpg');
console.log(probe.sync(data));

images-size

安装

yarn add -D images-size @types/images-size

基础使用

import imgsize from 'images-size'

const {width, height} = imgsize.get('img.jpg')