展示
布局样式
<script src="https://cdn.bootcss.com/vConsole/3.3.4/vconsole.min.js"></script>
<style>
canvas{
width: 200px;
height: 200px;
opacity: 0;
}
.upload{
width: 200px;
height: 200px;
display: inline-block;
border-radius: 10px;
border: 1px dashed rgb(122, 201, 221);
background-color: rgb(188, 224, 223);
overflow: hidden;
position: relative;
display: inline-block;
}
#upload-img{
width: 100%;
height: 100%;
object-fit: cover;
}
input[type='file']{
position: absolute;
top: 0;
left: 0;
font-size: 1000%;
opacity: 0;
}
</style>
<div>
<div class="upload">
<img id="upload-img" src="" alt="">
<input id="file" type="file" capture="camera" accept="image/*" onchange="handleFileChange(this)">
</div>
<canvas id="canvas" width="200" height="200"></canvas>
</div>
<script src="https://cdn.jsdelivr.net/npm/exif-js"></script>
逻辑
var vConsole = new VConsole();
async function handleFileChange(element) {
var file = element.files[0];
const or = await getImageTag(file);
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onloadend = function () {
const result = this.result;
const img = new Image();
img.src = result;
img.onload = function () {
const data = getRotateImg(img, or);
const f = dataURLtoFile(data);
};
};
}
const getImageTag = (file) => {
if (!file) return 0;
return new Promise((resolve, reject) => {
EXIF.getData(file, function () {
const o = EXIF.getTag(this, 'Orientation');
resolve(o);
});
});
};
function getRotateImg(img, or) {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const width = img.width;
const height = img.height;
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, 0, 0, width, height);
switch (or) {
case 6:
return rotateImg(img, 'right', canvas);
break;
case 8:
return rotateImg(img, 'left', canvas);
break;
case 3:
return rotateImg(img, 'right', canvas, 2);
break;
default:
break;
}
}
const rotateImg = (img, dir = 'right', canvas, s = 1) => {
const MIN_STEP = 0;
const MAX_STEP = 3;
const width = canvas.width || img.width;
const height = canvas.height || img.height;
let step = 0;
if (dir === 'right') {
step += s;
step > MAX_STEP && (step = MIN_STEP);
} else {
step -= s;
step < MIN_STEP && (step = MAX_STEP);
}
const degree = step * 90 * Math.PI / 180;
const ctx = canvas.getContext('2d');
switch (step) {
case 1:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(img, 0, -height, width, height);
break;
case 2:
canvas.width = width;
canvas.height = height;
ctx.rotate(degree);
ctx.drawImage(img, -width, -height, width, height);
break;
case 3:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(img, -width, 0, width, height);
break;
default:
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, 0, 0, width, height);
break;
}
return canvas.toDataURL('image/jpg');
};
const dataURLtoFile = (dataUrl) => {
const filename = `img${
Date.now()}`;
const arr = dataUrl.split(',');
const mime = arr[0].match(/:(.*?);/)[1];
const bstr = atob(arr[1]);
let n = bstr.length;
const u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, {
type: mime
});
};