日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

js 图片压缩上传(base64位)以及上传类型分类

發布時間:2025/3/15 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 js 图片压缩上传(base64位)以及上传类型分类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、input file上傳類型

1.指明只需要圖片

<input type="file" accept='image/*'>

2.指明需要多張圖片

<input type="file" multiple accept='image/*'>

3.指明調用攝像頭獲取圖片

<input type="file" capture='camera' accept='image/*'>

4.調用攝像頭并獲取多張圖片(攝像頭只能單張獲取,multiple無效)

<!-- multiple 無效 --> <input type="file" multiple capture='camera' accept='image/*'>

?二、圖片壓縮上傳

(1)移動端IOS上傳的某些圖片預覽時發生了旋轉?

  你會發現手機截圖,網絡圖片都不會有旋轉問題,只有手機拍攝的才有,這就跟拍攝時的角度有問題了,所以我們通過??exif.js??獲取角度(Orientation)的值,所獲值分別需要旋轉的角度如下:

        

旋轉角度參數值
1
順時針90°6
逆時針90°8
180°3

?

<!DOCTYPE html> <html><head><meta charset="UTF-8"><title></title><meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /><script src="js/jquery-1.11.3.min.js"></script><script src="js/exif.js"></script><style>#preview{width:100px;height:110px;}</style></head><body><input type="file" id="files" ><img src="blank.gif" id="preview"><script>var ipt = document.getElementById('files'),img = document.getElementById('preview'),Orientation = null; ipt.onchange = function () {var file = ipt.files[0],reader = new FileReader(),image = new Image();if(file){EXIF.getData(file, function() { Orientation = EXIF.getTag(this, 'Orientation');console.log(Orientation);});reader.onload = function (ev) {image.src = ev.target.result;image.onload = function () {var imgWidth = this.width,imgHeight = this.height;// 控制上傳圖片的寬高if(imgWidth > imgHeight && imgWidth > 600){imgWidth = 600;imgHeight = Math.ceil(600 * this.height / this.width);}else if(imgWidth < imgHeight && imgHeight > 600){imgWidth = Math.ceil(600 * this.width / this.height);imgHeight = 600;}var canvas = document.createElement("canvas"),ctx = canvas.getContext('2d');canvas.width = imgWidth;canvas.height = imgHeight;if(Orientation && Orientation != 1){switch(Orientation){case 6: // 旋轉90度 canvas.width = imgHeight; canvas.height = imgWidth; ctx.rotate(Math.PI / 2); ctx.drawImage(this, 0, -imgHeight, imgWidth, imgHeight);break;case 3: // 旋轉180度 ctx.rotate(Math.PI); ctx.drawImage(this, -imgWidth, -imgHeight, imgWidth, imgHeight);break;case 8: // 旋轉-90度 canvas.width = imgHeight; canvas.height = imgWidth; ctx.rotate(3 * Math.PI / 2); ctx.drawImage(this, -imgWidth, 0, imgWidth, imgHeight);break;}}else{ctx.drawImage(this, 0, 0, imgWidth, imgHeight);}var newImageData = canvas.toDataURL("image/jpeg", 0.6);$("img").attr("src", newImageData.replace("data:base64", "data:image/jpeg;base64"));}}reader.readAsDataURL(file);} } </script></body> </html>

?

轉載于:https://www.cnblogs.com/wj19940520/p/10207321.html

總結

以上是生活随笔為你收集整理的js 图片压缩上传(base64位)以及上传类型分类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。