Skip to content

Commit b4fee04

Browse files
committed
feat: add zjc clipper
1 parent 8635b9e commit b4fee04

File tree

3 files changed

+140
-1
lines changed

3 files changed

+140
-1
lines changed

_posts/2024-10-09-zjc-clipper.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
layout: pure
3+
title: 珠江城小学学籍照片裁剪
4+
---
5+
6+
<style>
7+
body {
8+
font-size: 16px;
9+
}
10+
.container {
11+
max-width: 600px;
12+
margin: 0 auto;
13+
text-align: center;
14+
}
15+
img {
16+
max-width: 100%;
17+
}
18+
#cropper {
19+
margin-top: 10px;
20+
}
21+
</style>
22+
<link
23+
rel="stylesheet"
24+
href="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.5.12/cropper.min.css"
25+
/>
26+
<div class="container">
27+
<div class="btns">
28+
<input type="file" id="inputImage" accept="image/*" />
29+
<button id="exportBtn">导出图片</button>
30+
</div>
31+
<!-- <div class="btns">
32+
<input id="unsharpAmount" value="160" />
33+
<input id="unsharpThreshold" value="0.1" />
34+
</div> -->
35+
<br />
36+
<img id="image" src="" style="display: none" />
37+
<div id="cropper"></div>
38+
<br />
39+
</div>
40+
<script src="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.5.12/cropper.min.js"></script>
41+
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.0.0/html2canvas.min.js"></script>
42+
<script src="https://cdnjs.cloudflare.com/ajax/libs/pica/7.0.0/pica.min.js"></script>
43+
<script src="/resource/2024/2024-10-09-zjc-clipper-app.js"></script>

resource/2021/1117_gps.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ var pageControl = {
106106
//this.changeMapTo('baidu');
107107
//点击地图显示坐标
108108
map.on("click", this.onMapClick);
109-
110109
},
111110
generateMarker: function (pictureDetail, imgSrc) {
112111
const { lon, lat } = this.getPointByPictureDetail(pictureDetail);
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
document.addEventListener("DOMContentLoaded", () => {
2+
const inputImage = document.getElementById("inputImage");
3+
const image = document.getElementById("image");
4+
const cropper = document.getElementById("cropper");
5+
const exportBtn = document.getElementById("exportBtn");
6+
7+
let cropperInstance;
8+
9+
inputImage.addEventListener("change", (e) => {
10+
const files = e.target.files;
11+
const done = (url) => {
12+
inputImage.value = "";
13+
image.src = url;
14+
image.style.display = "block";
15+
16+
if (cropperInstance) {
17+
cropperInstance.destroy();
18+
}
19+
20+
cropperInstance = new Cropper(image, {
21+
aspectRatio: 3 / 4,
22+
viewMode: 1,
23+
dragMode: "move",
24+
});
25+
};
26+
27+
const reader = new FileReader();
28+
reader.onload = (e) => done(reader.result);
29+
reader.readAsDataURL(files[0]);
30+
});
31+
32+
exportBtn.addEventListener("click", () => {
33+
const canvas = cropperInstance.getCroppedCanvas({
34+
width: 150,
35+
height: 200,
36+
});
37+
38+
canvas.toBlob(
39+
(blob) => {
40+
const reader = new FileReader();
41+
reader.onload = (e) => {
42+
const imgData = e.target.result;
43+
44+
// Compress image using pica library
45+
const targetCanvas = document.createElement("canvas");
46+
targetCanvas.width = 150;
47+
targetCanvas.height = 200;
48+
49+
const picaImg = pica();
50+
const unsharpAmount =
51+
+document.getElementById("unsharpAmount")?.value || 0;
52+
const unsharpThreshold =
53+
+document.getElementById("unsharpThreshold")?.value || 0;
54+
console.log(unsharpAmount, unsharpThreshold);
55+
picaImg
56+
.resize(canvas, targetCanvas, {
57+
unsharpAmount: unsharpAmount,
58+
unsharpThreshold: unsharpThreshold,
59+
})
60+
.then((result) => {
61+
const compressedBlob = dataURLtoBlob(
62+
targetCanvas.toDataURL("image/jpeg"),
63+
"image/jpeg"
64+
);
65+
66+
// Ensure size is below 64KB
67+
if (compressedBlob.size <= 64 * 1024) {
68+
// Create a download link
69+
const link = document.createElement("a");
70+
link.href = URL.createObjectURL(compressedBlob);
71+
link.download = "cropped-image.jpg";
72+
link.click();
73+
} else {
74+
alert(
75+
"Compressed image size is still above 64KB. Please try again."
76+
);
77+
}
78+
})
79+
.catch((err) => console.error(err));
80+
};
81+
reader.readAsDataURL(blob);
82+
},
83+
"image/jpeg",
84+
0.9
85+
); // Adjust quality here, 0.7 is 70% quality
86+
});
87+
88+
function dataURLtoBlob(dataUrl, mimeType) {
89+
const byteString = atob(dataUrl.split(",")[1]);
90+
const ab = new ArrayBuffer(byteString.length);
91+
const ia = new Uint8Array(ab);
92+
for (let i = 0; i < byteString.length; i++) {
93+
ia[i] = byteString.charCodeAt(i);
94+
}
95+
return new Blob([ab], { type: mimeType });
96+
}
97+
});

0 commit comments

Comments
 (0)