#å ³äºå¦ä½å®ç°å缩
æ¥é MDNæ¾å°çæ¯è¿ä¸ªAPIï¼
HTMLCanvasElement.toBlob()
æ¹æ³åé
Blob
对象ï¼ç¨ä»¥å±ç¤ºcanvasä¸çå¾çï¼è¿ä¸ªå¾çæ件å¯ä»¥è¢«ç¼åæä¿åå°æ¬å°ï¼ç±ç¨æ·ä»£ç端èªè¡å³å®ãå¦ä¸ç¹å«ææï¼å¾ççç±»åé»è®¤ä¸º image/pngï¼å辨ç为96dpiã
// callbackå¤çblob主è¦ï¼typeå¾çç±»åï¼encoderOptionså¾çå缩æ¯ä¾ 0-1
canvas.toBlob(callback, type, encoderOptions);
å¤å¶ä»£ç
#ææå¾
è¿éçå°å缩ææå·²ç»ç±åæ¥ç8.5Mbå°äº1.6Mbå·¦å³,è¿éæ个é®é¢Macä¸é¢çå缩ææ没Win好(å°´å°¬äº)ã
#å®ç°ä»£ç
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
body{
padding: 20px 0;
#fileSelect{
margin-top: 10px;
padding: 10px 5px;
border: 1px solid chartreuse;
border-radius: 5px;
background: green;
color: white;
text-decoration: none;
#fileSelect:hover{
opacity: 0.8;
</style>
</head>
<input type="file" id="fileElem" multiple accept="image/*" style=""
onchange="handleFiles(event,this.files)">
<a href="#" id="fileSelect">ç¹å»ä¸ä¼ </a>
</body>
<script>
window.onload = function () {
var fileSelect = document.getElementById("fileSelect"),
fileElem = document.getElementById("fileElem");
fileSelect.addEventListener("click", function (e) {
if (fileElem) {
fileElem.click(); // inputæ件ä¸ä¼
e.preventDefault();
}, false);
function handleFiles(e, files) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
var imageType = /^image\//;
if (!imageType.test(file.type)) {
continue;
console.log(file);
const width = 500;
const height = 300;
const fileName = file.name;
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = event => {
const img = new Image();
img.src = reader.result;
img.onload = () => {
const elem = document.createElement('canvas');
elem.width = width;
elem.height = height;
const ctx = elem.getContext('2d');
// canvas大å°è®¾ç½®
ctx.drawImage(img, 0, 0, width, height);
// æ¤å
¥å
容
document.body.append(elem);
//--------- å¦æä¸éè¦èµæºä¸ä¼ ï¼åªéæ¾ç¤ºï¼ä¸é¢ä»£ç å¯ä»¥ä¸è¦äº------
ctx.canvas.toBlob((blob) => {
const file = new File([blob], fileName, {
type: 'image/jpeg',
lastModified: Date.now()
console.log(file); // file为å缩åçå¾ç
}, 'image/jpeg', 10e-9);
// ----------------------
reader.onerror = error => console.log(error);
</script>
</html>
å¤å¶ä»£ç
#æµç¨
1. new FileReader()
é¦å
éè¦çæ¯å©ç¨è¿ä¸ªAPIæ¥è¯»åæ件ï¼å®ä¾åä¹åéç¨ readAsDataURL çæ¹å¼æ¥è¯»åå¾ççèµæºè·¯å¾,è¿éçå
¼å®¹æ§ä¸éï¼åéäºå ä¹æææµè§å¨çæ¬åå¹³å°ã
reader.readAsDataURL(file); //è¿æ ·è¯»åå¾çï¼ä¸è¿°ä¾åå¯ä»¥å¤éæä½ï¼æ以循ç¯äºä¸
2.reader.onloadçåè°ä¸å建å¾ç
// å建å¾çå
容
const img = new Image();
// å°å¾çå°åç»å®å°base64,è¿ééå°ä¸ä¸ªé®é¢éç¨event.target.resultåä¸å°å½åï¼readerä¸é¢æresultå±æ§å¯ç¨
img.src = reader.result;
3.å建ç»å¸ï¼ç»å¶å¾çå
容
// å建canvasç»å¸
const elem = document.createElement('canvas');
// 设置ç»å¸å¤§å°
elem.width = width;
elem.height = height;
//è·åðï¸
const ctx = elem.getContext('2d')
// ç»å¶
ctx.drawImage(img, 0, 0, width, height); // drawImageè¿éæ¥çMDNææ¡£å³å¯
// æ¤å
¥é¡µé¢
document.body.append(elem);
å¤å¶ä»£ç
4.åéç¹å缩ï¼å°canvaså缩大å°è½¬ä¸ºimage对象
ctx.canvas.toBlob((blob) => {
const file = new File([blob], fileName, {
type: 'image/jpeg', // è¿éçæ ¼å¼å¯ä»¥æ¯jpgï¼pngï¼gifï¼pngçï¼é½è¯äºä¸ï¼å缩大å°ä¸å
lastModified: Date.now()
console.log(file);
}, 'image/jpeg', 10e-9); // è¿éç10e-9å¾å°äº æé0-1 0æ¯æ大å缩æ¯çï¼ 1è¿éæç¹çå¦åå¤§äº è¶
è¿1 æè
å°äº0 大å°å¤§æ¦å缩äº6æå·¦å³
è¿éçå°ä¸ä¸ªå
¼å®¹æ§çé®é¢,主è¦èèçSarari IOSä¸ï¼è°å«å®¢æ·ç¨å¢ï¼
// MDNç»é¤äºå
¼å®¹ä»£ç ï¼å¤§ä½æ¯æ¯å¨Objectååæ·»å äºtoBlobå½æ°å®ç°ï¼ç²è´´æ·»å 就好
if (!HTMLCanvasElement.prototype.toBlob) {
Object.defineProperty(HTMLCanvasElement.prototype, 'toBlob', {
value: function (callback, type, quality) {
var dataURL = this.toDataURL(type, quality).split(',')[1];
setTimeout(function() {
var binStr = atob( dataURL ),
len = binStr.length,
arr = new Uint8Array(len);
for (var i = 0; i < len; i++ ) {
arr[i] = binStr.charCodeAt(i);
callback( new Blob( [arr], {type: type || 'image/jpeg'} ) );
å¤å¶ä»£ç
#ç»è¯
ç½ä¸èµææºå¤ï¼æ²¡æ¾å°ä¸ä¸å°±è½è·çï¼æ¥æ¥ææå®ç°äºåè½ï¼è¿è½ç¨ï¼æ¯ççã -- 2019å¹´3æ25æ¥ å¨è¾¹æ 人
#åèèµæ