利用html5 canvas實現純前端上傳圖片的裁剪

來源:互聯網
上載者:User

標籤:

今天跟大家分享一個前端裁剪圖片的方法.

許多網站都有設定帳戶圖片的功能,使用者可以選擇一張本地的圖片,然後用網站的裁剪工具進行裁剪,然後設定大小,位置合適的頭像.
當然,網上也有一些用js寫的諸如此類裁剪的外掛程式,但是有許多都是前端將圖片的一些裁剪參數(如座標)傳給後台,有java程式員進行真正的圖片裁剪.今天自己研究了一些,做了一個純前端裁剪的demo,如下:

1.html部分:
<div>
<input type="file" id="imgFile">
</div>
<div style="width: 300px;height: 300px;position: absolute;left: 300px;top: 0;display: inline-block">
<img id="demoImg" style="height: 300px;width: 300px" >
<div style="width: 150px;height: 150px;border: 1px solid #4fb8e3;position: absolute;left: 0;top: 0;z-index: 1000" id="chooseBox"></div>
</div>
<div style="position: absolute;left: 700px;top: 0">
<canvas id="myCan" width="150" height="150"></canvas>
<button id="cut">裁剪檔案</button>
<button id="btn">後台返回擷取裁剪後的檔案</button>
<img id="returnImg" >
</div>

頁面初始如下:

2.第二步進行檔案選擇後的預覽操作

var can=document.getElementById("myCan");
var btn=document.getElementById("btn");
var returnImg=document.getElementById("returnImg");
var ctx=can.getContext("2d");

$(‘#imgFile‘).change(function () {
var file=$(‘#imgFile‘)[0].files[0];
var reader=new FileReader();
reader.onload= function (e) {
$(‘#demoImg‘).attr(‘src‘, e.target.result);
};
reader.readAsDataURL(file);
});

3.進行裁剪

注意到任務映像裡面有一個藍色邊框的選擇框(大小固定,沒有做縮放),將藍色選框移動到你需要的位置,點擊裁剪檔案
$(‘#cut‘).click(function () {
var newX=$(‘#chooseBox‘).position().left*3.45;
var newY=$(‘#chooseBox‘).position().top*2.6;
var img=document.getElementById("demoImg");
console.log(newX,newY);
ctx.drawImage(img,newX,newY,150*3.45,150*2.6,0,0,150,150);
});


4.將裁剪後的圖片傳給後台,並將返回的圖片展示
btn.onclick=function () {
var data=can.toDataURL();
data=data.split(‘,‘)[1];
data=window.atob(data);
var ia = new Uint8Array(data.length);
for (var i = 0; i < data.length; i++) {
ia[i] = data.charCodeAt(i);
}
var blob=new Blob([ia],{type:"image/png",endings:‘transparent‘});
var fd=new FormData();
console.log(blob);
fd.append(‘avatarFile‘,blob,‘image.png‘);
var httprequest=new XMLHttpRequest();
httprequest.open(‘POST‘,‘/guest/avatar‘,true);
httprequest.send(fd);
httprequest.onreadystatechange= function () {
if(httprequest.status==200 && httprequest.readyState==4){
console.log(httprequest.responseText);
$(‘#returnImg‘).attr(‘src‘,‘/images/‘+JSON.parse(httprequest.responseText).json);
}
};
};


整個demo的注意點有四點:

1.圖片上傳之後使用fileReader將圖片檔案轉換成base64 png格式圖片,從而實現預覽

2.映像選擇框的移動,mousedown,mouseup,mouseup事件的配合使用

3.html5 canvas實現圖片裁剪後的效果展示

4.利用html5 formData,將圖片檔案轉換成blob對象,傳給後台

完整源碼請移步我的github項目地址:
https://github.com/hyq2015/canvas_crop_img

參考文檔:http://blog.csdn.net/cuixiping/article/details/45932793

     1190000000754560

http://blog.csdn.net/oscar999/article/details/36373183

利用html5 canvas實現純前端上傳圖片的裁剪

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.