# lrz

> 前端本地客户端压缩图片，兼容IOS，Android，PC、自动按需加载文件

Latest version **4.9.41** (published 2020-09-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install lrz
pnpm add lrz
yarn add lrz
bun add lrz
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.9.41 |
| Published | 2020-09-28 |
| First published | 2015-08-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 459.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3220 |
| Author | think2011 |
| Maintainers | think2011 |
| Keywords | lrz, localResizeIMG, 前端压缩图片, 前端图片压缩, 压缩图片 |

## Links

- npm: https://www.npmjs.com/package/lrz
- Repository: https://github.com/think2011/localResizeIMG
- Homepage: https://github.com/think2011/localResizeIMG#readme
- Issues: https://github.com/think2011/localResizeIMG/issues
- npm.io page: https://npm.io/package/lrz

## Dependencies (2)

- [gulp-insert](https://npm.io/package/gulp-insert.md) ^0.5.0
- [gulp-rename](https://npm.io/package/gulp-rename.md) ^1.2.2

## Alternatives

- [exif-parser](https://npm.io/package/exif-parser.md) — 3.8M weekly downloads
- [vite-plugin-compression](https://npm.io/package/vite-plugin-compression.md) — 569.5K weekly downloads
- [pica](https://npm.io/package/pica.md) — 442.4K weekly downloads
- [@reportportal/client-javascript](https://npm.io/package/@reportportal/client-javascript.md) — 408.8K weekly downloads
- [@tldraw/state](https://npm.io/package/@tldraw/state.md) — 316.0K weekly downloads

## Recent versions

- 4.9.41 (latest) — 2020-09-28
- 4.9.40 — 2016-10-20
- 4.9.37 — 2016-08-19
- 4.9.36 — 2016-04-08
- 4.9.35 — 2016-03-18
- 4.8.35 — 2015-11-24
- 4.7.35 — 2015-11-18
- 4.7.32 — 2015-11-18
- 4.7.30 — 2015-11-18
- 4.7.29 — 2015-11-17
- 4.7.27 — 2015-11-17
- 4.6.27 — 2015-11-16
- 4.6.25 — 2015-11-16
- 4.6.24 — 2015-11-14
- 4.5.23 — 2015-11-13
- … 19 more at https://npm.io/package/lrz/versions

## README

[![Build Status](https://travis-ci.org/think2011/localResizeIMG.svg?branch=master)](https://travis-ci.org/think2011/localResizeIMG)
[![npm version](https://img.shields.io/npm/v/lrz.svg)](https://www.npmjs.com/package/lrz)
[![npm](https://img.shields.io/npm/l/express.svg)]()

# 🚨重要!!

很抱歉，这个项目已不再维护了，可能很长一段时间都不会更新了。

# 演示

![](http://think2011.github.io/localResizeIMG/test/demo.gif)

# 试试

![](https://raw.github.com/think2011/localResizeIMG/master/test/qrcode.png)


[点我直接进入演示页面](http://think2011.github.io/localResizeIMG/test/)

# 简述
在客户端压缩好要上传的图片可以节省带宽更快的发送给后端，特别适合在移动设备上使用。

# 为什么需要

1. 已踩过很多坑，经过几个版本迭代，以及很多很多网友的反馈帮助、机型测试
    * 图片扭曲、某些设备不自动旋转图片方向，没有jpeg压缩算法..
    * 不支持new Blob,formData构造的文件size为0..
    * 还有某些机型和浏览器（例如QQX5浏览器）莫名其妙的BUG..
    
2. 按需加载（会根据对应设备自动异步载入JS文件，节省不必要带宽）

3. 原生JS编写，不依赖例如`jquery`等第三方库，支持AMD or CMD规范。

> 尽管如此，在某些 `Android` 下依然有莫名其妙的问题，在您使用前，请一定大致浏览下 [issues](https://github.com/think2011/localResizeIMG/issues)

# 如何获取

通过以下方式都可以下载：

1. 执行`npm i lrz`（推荐）
2. 执行`bower install lrz`

接着在页面中引入
```html
<script src="./dist/lrz.bundle.js"></script>
```

# 如何使用

### 方式1:

如果您的图片来自用户拍摄或者上传的，您需要一个`input file`来获取图片。

```html
<input id="file" type="file" accept="image/*" />
```

接着通过change事件可以得到用户选择的图片
```js
document.querySelector('#file').addEventListener('change', function () {
	lrz(this.files[0])
        .then(function (rst) {
            // 处理成功会执行
            console.log(rst);
        })
        .catch(function (err) {
            // 处理失败会执行
        })
        .always(function () {
            // 不管是成功失败，都会执行
        });
});
```

### 方式2：

如果您的图片不是来自用户上传的，那么也可以直接传入图片路径。

```js
lrz('./xxx/xx/x.png')
        .then(function (rst) {
            // 处理成功会执行
        })
        .catch(function (err){
            // 处理失败会执行
        })
        .always(function () {
            // 不管是成功失败，都会执行
        });
```

# 后端处理

[后端处理请查看WIKI。](https://github.com/think2011/localResizeIMG/wiki)


# API

[具体参数说明请查看WIKI。](https://github.com/think2011/localResizeIMG/wiki)

# 兼容性

IE10以上及大部分非IE浏览器（chrome、微信什么的）

# FAQ

[有疑问请直接在 issues 中提问](https://github.com/think2011/localResizeIMG/issues)

```
请一定记得附上以下内容：💡
请一定记得附上以下内容：🙈
请一定记得附上以下内容：💡

平台：微信..
设备：iPhone5 IOS7..
问题：问题描述..
```

* Q：有时拍摄完照片后，页面自动刷新或闪退了。
* A：虽然已作了优化处理，但内存似乎还是爆掉了，常见于低配android手机，建议每次只处理一张图片。

* Q: 怎么批量上传图片?
* A: 您可以自己写个循环来传入用户多选的图片，但在移动端上请谨慎处理，原因同上。

* Q: 直接传入图片路径的无法生成图片
* A: 可能是跨域的问题，具体请看[CORS_enabled_image](https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image)

* Q: 想要商用可以吗？
* A: 没问题，但请留意issue里已知的问题。

# 感谢

* [dwandw](https://github.com/dwandw)
* [yourlin](https://github.com/yourlin)
* [wxt2005](https://github.com/wxt2005)
* [LingRen](https://github.com/LingRen)

以上在之前的版本帮忙参与维护的朋友，以及提出问题的朋友们，真的真的很感谢你们。：D

---
_Source: https://npm.io/package/lrz · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
