前端图片裁剪 cropperjs的简单使用

https://github.com/fengyuanchen/cropperjs
演示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<!DOCTYPE html>
<html lang="zh">
<head>
<title>前端图片裁剪 cropperjs</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://unpkg.com/cropperjs@1.5.12/dist/cropper.css">
</head>
<body>
<input type="file" id="input" accept="image/*">
<img id="after-image" src="" style="display: block;">
<button onclick="ok()" style="display: block;">裁剪</button>

<hr />
处理后:
<img id="before-image" src="" style="display: block;">





<script src="https://unpkg.com/cropperjs@1.5.12"></script>
<script type="text/javascript">
const Aimage = document.getElementById('after-image');
let cropper;
document.getElementById('input').addEventListener('change', (e) => {
const file = e.target.files[0];

const reader = new FileReader(); // 创建FileReader对象(文件对象)
console.log(reader);
reader.readAsDataURL(file);
reader.onload = (e) => {
console.log("读取成功", e.target.result);
Aimage.src = e.target.result;

cropper = new Cropper(Aimage, {
viewMode: 1
});
};
})

function ok() {
const Bimage = document.getElementById('before-image');
Bimage.src = cropper.getCroppedCanvas({
imageSmoothingQuality: 'high'
}).toDataURL('image/png')
console.log("处理后:",Bimage.src);
}
</script>
</body>
</html>

前端图片裁剪 cropperjs的简单使用
https://github.com/chergn/chergn.github.io/475422dd8cf8/
作者
全易
发布于
2024年3月28日
许可协议