npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@2080code/pic-processer

v0.0.4

Published

图片压缩、改变图片尺寸、File 和 dataURL 互转

Readme

@2080code/pic-processer

NPM Version npm package minimized gzipped size NPM Last Update NPM Downloads

介绍

用来对图片做压缩,把 File 对象转为 dataURL,又或者把 dataURL 转为 File 对象。


安装

npm install @2080code/pic-processer

使用

import PicProcesser from '@2080code/pic-processer'

<script src="lib/pic-processer.min.js"></script>

例子:

// 使用前先实例化一个对象:
const picProcesser = new PicProcesser()
// 应用:
picProcesser.getDataURL(file)
picProcesser.compress(file, 0.2)
picProcesser.getDataURL(file)

方法

compress

压缩图片质量、改变图片尺寸,输出 dataURL

picProcesser.compress(file, 0.2, 1000, 'image/png')
参数

参数|类型|默认值|说明 ---|---|---|--- file | File | null | 图片文件 ratio | number | 0.5 | 压缩比例,0-1 之间的小数 maxWidth | number | null | 图片宽度上限,>1的整数,单位px,0null 表示不限制,输出图片的高度会等比例缩放 mime | string | image/jpeg | 输出图片类型

返回

返回值|类型|说明 ---|---|--- Promise<string> | Promise<string> | 压缩后的图片 dataURL 编码

注意:因为是借助 canvas 渲染输出,几乎是重绘了图片,所以如果压缩比率(ratio)设置得太高,最终文件字节可能会比原图更大


getDataURL

File 转 dataURL

picProcesser.getDataURL(file)
参数

参数|类型|默认值|说明 ---|---|---|--- file | File|Blob | null | 图片文件

返回

返回值|类型|说明 ---|---|--- Promise<FileReader> | Promise<FileReader> | 完整的 FileReader 对象,通过result属性获取 base64 编码


dataURLtoFile

dataURL 转 File 对象

picProcesser.dataURLtoFile('export.jpg', dataURL)
参数

参数|类型|默认值|说明 ---|---|---|--- fileName | string | null | 输出文件名 dataURL | string | null | base64编码

返回

返回值|类型|说明 ---|---|--- Promise<File> | Promise<File> | File 对象