js壓縮上傳圖片base64長度

來源:互聯網
上載者:User

標籤:格式   val   uri   str   並發   參數   div   das   create   

im發送圖片,現將圖片壓縮再上傳

1) 調用 FileReader 的 reader.readAsDataURL(img); 方法, 在其onload事件中, 將使用者選擇的圖片讀入 Image對象.

2) 在image對象的 onload 事件中, 通過 canvas 的 canvas.getContext(‘2d‘) 的 drawImage 方法, 將Image 改變大小繪製到canvas上.

3) 通過 canvas.toDataURL("image/jpeg", 1); 方法, 將圖片變成base64字串, 放到send_image_value

toDataURL

canvas.toDataURL(type, encoderOptions); 

傳回值

  包含 data URI 的DOMString

type

  圖片格式,預設為 image/png

encoderOptions

  在指定圖片格式為 image/jpeg 或 image/webp的情況下,可以從 0 到 1 的區間內選擇圖片的品質。如果超出取值範圍,將會使用預設值 0.92。其他參數會被忽略。

html

<input id="sendImage" title="send a picture"  type="file" accept="image/*" onchange="imgChange(this)"><input type="hidden" value="" id="send_image_value"><img id="showLoadingimg" src="‘+CHAT_SITE_URL+‘/templates/default/images/loading.gif" style="position:relative;left:200px;top:200px;z-index:999;display:none;">

  

js

function imgChange(e){//檢查是否有檔案被選中        if (e.files.length != 0) {        var file = e.files[0],        fileType = file.type,            reader = new FileReader();            if (!reader) {                e.value = ‘‘;                return;            };            var size = file.size;            var max_size = 2*1024*1024;            if(size>max_size){            e.value = ‘‘;            $("#send_alert").html(‘file is too large(>2M)‘);            return;            }            $("#showLoadingimg").show();            reader.onload = function(e) {                //讀取成功,顯示到頁面並發送到伺服器                e.value = ‘‘;                var org_img = e.target.result;            var image_base64 = org_img;            if(size>1024*80){//>80K的            var img = new Image();              img.src = org_img;              img.onload=function(){            var scale = 1;                            if(this.width > 300 || this.height > 300){                                  if(this.width > this.height){                                    scale = 300 / this.width;                              }else{                                    scale = 300 / this.height;                                }                            }              var canvas = document.createElement("canvas"), drawer = canvas.getContext("2d");             if(scale!=1) {//按最大高度等比縮放                img.width  *= scale;                 img.height *= scale;               }             canvas.width = img.width;            canvas.height = img.width * (img.height / img.width);                drawer.drawImage(img, 0, 0, canvas.width, canvas.height);                var tmp_code = image_base64 = canvas.toDataURL(fileType);                 if(tmp_code!=‘data:,‘){                image_base64 = tmp_code;                }                img_send(image_base64);            };              }else{            img_send(image_base64);            }                            };            reader.readAsDataURL(file);        }    }    /**     *為將圖片賦值給訊息    **/    function img_send(image_base64){    if(image_base64!=‘data:,‘){    $("#send_image_value").val(image_base64);send_msg();$("#showLoadingimg").hide();$(‘#sendImage‘).val("");    }    }  

開始時,toDataURL擷取的值是data:,

$("#send_image_value").val(image_base64);
send_msg();

這兩句寫在

reader.onload方法的最下面這就導致圖片並沒有壓縮

因為img.onload還沒執行完

發送的還是原來的圖片

在調整後就可以實現圖片的壓縮了 

js壓縮上傳圖片base64長度

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.