Skip to content

Commit b65d484

Browse files
authored
移除 canvas 绘制图像的方向兼容处理 (#525)
* 移除针对 canvas 绘制图片时的 Orientation 兼容处理(已经进入 W3C 规范且支持良好)
1 parent 519a645 commit b65d484

File tree

7 files changed

+8026
-8489
lines changed

7 files changed

+8026
-8489
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ qiniu.compressImage(file, options).then(data => {
269269

270270
### qiniu.compressImage(file: File, options: object): Promise<CompressResult> (上传前图片压缩)
271271

272+
**在 v3.3.3 版本之前,该压缩行为会根据图片的 `Orientation(设备角度)` 信息对图片进行旋转处理,详细的信息可以参考**
273+
[issue:关于 canvas 绘制图像的方向兼容处理](https://github.com/qiniu/js-sdk/issues/522 )
274+
272275
```JavaScript
273276
const imgLink = qiniu.compressImage(file, options).then(res => {
274277
// res : {

package-lock.json

+2-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "qiniu-js",
33
"jsName": "qiniu",
4-
"version": "3.3.2",
4+
"version": "3.3.3",
55
"private": false,
66
"description": "Javascript SDK for Qiniu Resource (Cloud) Storage AP",
77
"main": "lib/index.js",
@@ -78,7 +78,6 @@
7878
"license": "MIT",
7979
"dependencies": {
8080
"@babel/runtime-corejs2": "^7.10.2",
81-
"exif-js": "^2.3.0",
8281
"querystring": "^0.2.1",
8382
"spark-md5": "^3.0.0"
8483
}

src/errors/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export enum QiniuErrorName {
1414
RemoveCacheFailed = 'RemoveCacheFailed',
1515

1616
// 图片压缩模块相关
17-
InvalidTransformOrientation = 'InvalidTransformOrientation',
1817
GetCanvasContextFailed = 'GetCanvasContextFailed',
1918
UnsupportedFileType = 'UnsupportedFileType',
2019

src/utils/compress.ts

+19-27
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { EXIF } from 'exif-js'
2-
31
import { QiniuErrorName, QiniuError } from '../errors'
42

5-
import { createObjectURL, getTransform } from './helper'
3+
import { createObjectURL } from './helper'
64

75
export interface CompressOptions {
86
quality?: number
@@ -117,30 +115,24 @@ class Compress {
117115

118116
getCanvas(img: HTMLImageElement): Promise<HTMLCanvasElement> {
119117
return new Promise((resolve, reject) => {
120-
// 通过得到图片的信息来调整显示方向以正确显示图片,主要解决 ios 系统上的图片会有旋转的问题
121-
EXIF.getData(img, () => {
122-
const orientation = EXIF.getTag(img, 'Orientation') || 1
123-
const { width, height, matrix } = getTransform(img, orientation)
124-
const canvas = document.createElement('canvas')
125-
const context = canvas.getContext('2d')
126-
if (!context) {
127-
reject(new QiniuError(
128-
QiniuErrorName.GetCanvasContextFailed,
129-
'context is null'
130-
))
131-
return
132-
}
133-
134-
canvas.width = width
135-
canvas.height = height
136-
137-
this.clear(context, width, height)
138-
context.save()
139-
context.transform(...matrix)
140-
context.drawImage(img, 0, 0)
141-
context.restore()
142-
resolve(canvas)
143-
})
118+
const canvas = document.createElement('canvas')
119+
const context = canvas.getContext('2d')
120+
121+
if (!context) {
122+
reject(new QiniuError(
123+
QiniuErrorName.GetCanvasContextFailed,
124+
'context is null'
125+
))
126+
return
127+
}
128+
129+
const { width, height } = img
130+
canvas.height = height
131+
canvas.width = width
132+
133+
this.clear(context, width, height)
134+
context.drawImage(img, 0, 0)
135+
resolve(canvas)
144136
})
145137
}
146138

src/utils/helper.ts

+1-74
Original file line numberDiff line numberDiff line change
@@ -337,79 +337,6 @@ export function getPutPolicy(token: string): PutPolicy {
337337

338338
export function createObjectURL(file: File) {
339339
const URL = window.URL || window.webkitURL || window.mozURL
340+
// FIXME: 需要 revokeObjectURL
340341
return URL.createObjectURL(file)
341342
}
342-
343-
export interface TransformValue {
344-
width: number
345-
height: number
346-
matrix: [number, number, number, number, number, number]
347-
}
348-
349-
export function getTransform(image: HTMLImageElement, orientation: number): TransformValue {
350-
const { width, height } = image
351-
352-
switch (orientation) {
353-
case 1:
354-
// default
355-
return {
356-
width,
357-
height,
358-
matrix: [1, 0, 0, 1, 0, 0]
359-
}
360-
case 2:
361-
// horizontal flip
362-
return {
363-
width,
364-
height,
365-
matrix: [-1, 0, 0, 1, width, 0]
366-
}
367-
case 3:
368-
// 180° rotated
369-
return {
370-
width,
371-
height,
372-
matrix: [-1, 0, 0, -1, width, height]
373-
}
374-
case 4:
375-
// vertical flip
376-
return {
377-
width,
378-
height,
379-
matrix: [1, 0, 0, -1, 0, height]
380-
}
381-
case 5:
382-
// vertical flip + -90° rotated
383-
return {
384-
width: height,
385-
height: width,
386-
matrix: [0, 1, 1, 0, 0, 0]
387-
}
388-
case 6:
389-
// -90° rotated
390-
return {
391-
width: height,
392-
height: width,
393-
matrix: [0, 1, -1, 0, height, 0]
394-
}
395-
case 7:
396-
// horizontal flip + -90° rotate
397-
return {
398-
width: height,
399-
height: width,
400-
matrix: [0, -1, -1, 0, height, width]
401-
}
402-
case 8:
403-
// 90° rotated
404-
return {
405-
width: height,
406-
height: width,
407-
matrix: [0, -1, 1, 0, 0, width]
408-
}
409-
default:
410-
throw new QiniuError(
411-
QiniuErrorName.InvalidTransformOrientation,
412-
`orientation ${orientation} is unavailable`
413-
)
414-
}
415-
}

0 commit comments

Comments
 (0)