Skip to content

Commit

Permalink
fix review & 更新打包
Browse files Browse the repository at this point in the history
  • Loading branch information
winddies committed Jun 10, 2020
1 parent 5c9b9b3 commit a5bb47e
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 162 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ npm-debug.log
dist
test/config.json
coverage
.tmp
lib
35 changes: 19 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ qiniu.compressImage(file, options).then(data => {
```JavaScript
const requestUrl = qiniu.createMkFileUrl(
uploadUrl,
file.size,
file,
key,
putExtra
);
Expand Down Expand Up @@ -271,6 +271,7 @@ qiniu.compressImage(file, options).then(data => {
multipart_params_obj[k[0]] = k[1]
}
```
###
### qiniu.compressImage(file: File, options: object) : Promise (上传前图片压缩)

```JavaScript
Expand All @@ -291,11 +292,13 @@ qiniu.compressImage(file, options).then(data => {
(注意:当 `maxWidth``maxHeight` 都不设置时,则采用原图尺寸大小)
* options.noCompressIfLarger: `boolean`,为 `true` 时如果发现压缩后图片大小比原来还大,则返回源图片(即输出的 dist 直接返回了输入的 file);默认 `false`,即保证图片尺寸符合要求,但不保证压缩后的图片体积一定变小

### qiniu.watermark(options: object, key: string, domain: string): string(水印)
### qiniu.watermark(options: object, entry?: Entry): string(水印)

返回添加水印后的图片地址
* **key** : 文件资源名
* **domain**: 为七牛空间(bucket)对应的域名,选择某个空间后,可通过"空间设置->基本设置->域名设置"查看获取,前端可以通过接口请求后端得到
* **entry**: `object`
* **entry.key** : 文件资源名
* **entry.domain**: 为七牛空间(bucket)对应的域名,选择某个空间后,可通过"空间设置->基本设置->域名设置"查看获取,前端可以通过接口请求后端得到


```JavaScript
Expand All @@ -306,7 +309,7 @@ qiniu.compressImage(file, options).then(data => {
gravity: 'SouthWest', // 水印位置,为以下参数[NorthWest、North、NorthEast、West、Center、East、SouthWest、South、SouthEast]之一
dx: 100, // 横轴边距,单位:像素(px)
dy: 100 // 纵轴边距,单位:像素(px)
}, key, domain) // key 为非必需参数,下同
}, entry) // entry 为非必需参数,下同
// imgLink 可以赋值给 html 的 img 元素的 src 属性,下同
Expand All @@ -326,12 +329,12 @@ qiniu.compressImage(file, options).then(data => {
dx: 100, // 横轴边距,单位:像素(px)
dy: 100, // 纵轴边距,单位:像素(px)
fill: '#FFF000' // 水印文字颜色,RGB格式,可以是颜色名称
}, key,domain)
}, entry)
```

options包含的具体水印参数解释见[水印(watermark)](https://developer.qiniu.com/dora/api/image-watermarking-processing-watermark)

### qiniu.imageView2(options: object, key: string, domain: string): string (缩略)
### qiniu.imageView2(options: object, entry?: Entry): string (缩略)

返回处理后的图片url

Expand All @@ -342,12 +345,12 @@ qiniu.compressImage(file, options).then(data => {
h: 100, // 具体含义由缩略模式决定
q: 100, // 新图的图像质量,取值范围:1-100
format: 'png' // 新图的输出格式,取值范围:jpg,gif,png,webp等
}, key, domain)
}, entry)
```

options包含的具体缩略参数解释见[图片基本处理(imageView2)](https://developer.qiniu.com/dora/api/basic-processing-images-imageview2)

### qiniu.imageMogr2(options: object, key: string, domain: string): string (图像高级处理)
### qiniu.imageMogr2(options: object, entry?: Entry): string (图像高级处理)

返回处理后的图片url

Expand All @@ -362,28 +365,28 @@ qiniu.compressImage(file, options).then(data => {
rotate: 20, // 旋转角度,取值范围1-360,缺省为不旋转。
format: 'png', // 新图的输出格式,取值范围:jpg,gif,png,webp等
blur: '3x5' // 高斯模糊参数
}, key, domain)
}, entry)
```

options包含的具体高级图像处理参数解释见[图像高级处理(imageMogr2)](https://developer.qiniu.com/dora/api/the-advanced-treatment-of-images-imagemogr2)

### qiniu.imageInfo(key: string, domain: string): Promise
### qiniu.imageInfo(entry: Entry): Promise

```JavaScript
qiniu.imageInfo(key, domain).then(res => {})
qiniu.imageInfo(entry).then(res => {})
```

具体 imageInfo 解释见[图片基本信息(imageInfo)](https://developer.qiniu.com/dora/api/pictures-basic-information-imageinfo)

### qiniu.exif(key: string, domain: string): Promise
### qiniu.exif(entry: Entry): Promise

```JavaScript
qiniu.exif(key, domain).then(res => {})
qiniu.exif(entry).then(res => {})
```

具体 exif 解释见[图片 EXIF 信息(exif)](https://developer.qiniu.com/dora/api/photo-exif-information-exif)

### qiniu.pipeline(fopArr: array, key: string, domain: string): string
### qiniu.pipeline(fopArr: array, entry?: Entry): string

```JavaScript
const fopArr = [{
Expand Down Expand Up @@ -443,7 +446,7 @@ qiniu.compressImage(file, options).then(data => {
// blur:'3x5'
// }];
const imgLink = qiniu.pipeline(fopArr, key, domain))
const imgLink = qiniu.pipeline(fopArr, entry))
```

fopArr包含的具体管道操作解释见[管道操作](https://developer.qiniu.com/dora/manual/processing-mechanism)
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"version": "2.6.0",
"private": false,
"description": "Javascript SDK for Qiniu Resource (Cloud) Storage AP",
"main": "dist/qiniu.min.js",
"types": "dist/types/index.d.ts",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"clean": "del \"./(lib|dist)\"",
"test": "jest --coverage",
"build": "npm run lint && webpack --optimize-minimize --config webpack.prod.js",
"build": "npm run clean && tsc && webpack --optimize-minimize --config webpack.prod.js",
"dev": "npm run lint && webpack-dev-server --open --config webpack.dev.js",
"lint": "tsc --noEmit && eslint --ext .ts",
"server": "node test/server.js"
Expand Down Expand Up @@ -47,6 +48,7 @@
"babel-runtime": "^6.26.0",
"body-parser": "^1.18.2",
"connect-multiparty": "^2.1.0",
"del-cli": "^3.0.1",
"es3ify-loader": "^0.2.0",
"eslint": "^6.8.0",
"eslint-plugin-import": "^2.20.2",
Expand Down
20 changes: 11 additions & 9 deletions src/__tests__/image.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { imageView2, imageMogr2, watermark, imageInfo } from '../image'
import { urlSafeBase64Encode } from '../base64'

describe('image func test', () => {
let domain = 'http://otxza7yo2.bkt.clouddn.com'
let key = 'test.png'
const entry = {
domain: 'http://otxza7yo2.bkt.clouddn.com',
key: 'test.png'
}

test('imageView2', () => {
let m = {
Expand All @@ -12,9 +14,9 @@ describe('image func test', () => {
'h': 450,
'q': 100
}
let url = imageView2(m, key, domain)
let url = imageView2(m, entry)
expect(url).toBe(
'http://otxza7yo2.bkt.clouddn.com/' + key + '?' +
'http://otxza7yo2.bkt.clouddn.com/' + entry.key + '?' +
'imageView2/' + encodeURIComponent(m.mode) +
'/h' + '/' + encodeURIComponent(m.h) +
'/q' + '/' + encodeURIComponent(m.q)
Expand All @@ -33,9 +35,9 @@ describe('image func test', () => {
blur: 1
}

let url = imageMogr2(m, key, domain)
let url = imageMogr2(m, entry)
expect(url).toBe(
'http://otxza7yo2.bkt.clouddn.com/' + key + '?imageMogr2/' +
'http://otxza7yo2.bkt.clouddn.com/' + entry.key + '?imageMogr2/' +
'thumbnail/1/strip/gravity/1/quality/1/crop/1/rotate/1/format/1/blur/1'
)
})
Expand All @@ -49,15 +51,15 @@ describe('image func test', () => {
dx: 100,
dy: 100
}
let url = watermark(m, key, domain)
let url = watermark(m, entry)
expect(url).toBe(
'http://otxza7yo2.bkt.clouddn.com/' + key + '?' +
'http://otxza7yo2.bkt.clouddn.com/' + entry.key + '?' +
'watermark/' + m.mode + '/image/' + urlSafeBase64Encode(m.image) +
'/dissolve/100/dx/100/dy/100'
)
m.mode = 3
expect(()=> {
watermark(m, key, domain)
watermark(m, entry)
}).toThrow('mode is wrong')
})
})
3 changes: 2 additions & 1 deletion src/__tests__/pool.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Pool } from "../pool"
import { ChunkInfo } from '../upload'

const m = jest.fn()
const task = (): Promise<void> => {
Expand All @@ -9,7 +10,7 @@ const task = (): Promise<void> => {
}

describe("test Pool for control concurrency", () => {
var pool = new Pool(task, 2)
var pool = new Pool<ChunkInfo>(task, 2)
test("pool.js", () => {
const chunk = new Blob()
const data = [
Expand Down
2 changes: 1 addition & 1 deletion src/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function utf8Encode(argString: any) {
throw new RangeError('Unmatched trail surrogate at ' + n)
}
let c2 = string.charCodeAt(++n)
if ((c2 & 0xfc00) ^ (0xdc00 > 0 as any)) {
if ((c2 & 0xfc00 ^ 0xdc00) > 0) {
throw new RangeError('Unmatched lead surrogate at ' + (n - 1))
}
c1 = ((c1 & 0x3ff) << 10) + (c2 & 0x3ff) + 0x10000
Expand Down
79 changes: 40 additions & 39 deletions src/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class Compress {
}

getCanvas(img: HTMLImageElement): Promise<HTMLCanvasElement> {
return new Promise(resolve => {
return new Promise((resolve, reject) => {
// 通过得到图片的信息来调整显示方向以正确显示图片,主要解决 ios 系统上的图片会有旋转的问题
EXIF.getData(img, () => {
const orientation = EXIF.getTag(img, 'Orientation') || 1
Expand All @@ -119,15 +119,15 @@ class Compress {
context.drawImage(img, 0, 0)
resolve(canvas)
} else {
throw new Error('context is null')
reject(new Error('context is null'))
}
})
})
}

doScale(source: HTMLCanvasElement, scale: number) {
async doScale(source: HTMLCanvasElement, scale: number) {
if (scale === 1) {
return Promise.resolve(source)
return source
}
// 不要一次性画图,通过设定的 step 次数,渐进式的画图,这样可以增加图片的清晰度,防止一次性画图导致的像素丢失严重
const sctx = source.getContext('2d')
Expand All @@ -143,47 +143,48 @@ class Compress {
const originHeight = height
mirror.width = width
mirror.height = height
if (mctx && sctx) {
let src!: CanvasImageSource
let context!: CanvasRenderingContext2D
for (let i = 0; i < steps; i++) {

let dw = width * factor || 0
let dh = height * factor || 0
// 到最后一步的时候 dw, dh 用目标缩放尺寸,否则会出现最后尺寸偏小的情况
if (i === steps - 1) {
dw = originWidth * scale
dh = originHeight * scale
}
if (!mctx || !sctx) {
throw new Error("mctx or sctx can't be null")
}

if (i % 2 === 0) {
src = source
context = mctx
} else {
src = mirror
context = sctx
}
// 每次画前都清空,避免图像重叠
this.clear(context, width, height)
context.drawImage(src, 0, 0, width, height, 0, 0, dw, dh)
width = dw
height = dh
let src!: CanvasImageSource
let context!: CanvasRenderingContext2D
for (let i = 0; i < steps; i++) {

let dw = width * factor | 0 // eslint-disable-line no-bitwise
let dh = height * factor | 0 // eslint-disable-line no-bitwise
// 到最后一步的时候 dw, dh 用目标缩放尺寸,否则会出现最后尺寸偏小的情况
if (i === steps - 1) {
dw = originWidth * scale
dh = originHeight * scale
}

const canvas = src === source ? mirror : source
// save data
const data = context.getImageData(0, 0, width, height)
if (i % 2 === 0) {
src = source
context = mctx
} else {
src = mirror
context = sctx
}
// 每次画前都清空,避免图像重叠
this.clear(context, width, height)
context.drawImage(src, 0, 0, width, height, 0, 0, dw, dh)
width = dw
height = dh
}

// resize
canvas.width = width
canvas.height = height
const canvas = src === source ? mirror : source
// save data
const data = context.getImageData(0, 0, width, height)

// store image data
context.putImageData(data, 0, 0)
// resize
canvas.width = width
canvas.height = height

return Promise.resolve(canvas)
}
throw new Error("mctx or sctx can't be null")
// store image data
context.putImageData(data, 0, 0)

return canvas
}

// 这里把 base64 字符串转为 blob 对象
Expand Down
Loading

0 comments on commit a5bb47e

Please sign in to comment.