js獲取圖片base64位編碼 還有一種用jq插件html2canvas

獲取圖片base64編碼
var img = "images/1.jpg";//imgurl 就是你的圖片路徑php

function getBase64Image(img) {
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, img.width, img.height);
var ext = img.src.substring(img.src.lastIndexOf(".")+1).toLowerCase();
var dataURL = canvas.toDataURL("image/"+ext);
return dataURL;
}css

var image = new Image();
image.src = img;
image.onload = function() {
var base64 = getBase64Image(image);
console.log(base64);
}html

 

 

jq插件html2canvas獲取 canvas

function print(link)
{
html2canvas( $('#theface') ,
{
onrendered: function(canvas)
{
//document.body.appendChild(canvas);
$('#down_button').attr( 'href' , canvas.toDataURL() ) ;
$('#down_button').attr( 'download' , 'myjobdeer.png' ) ;
$('#down_button').css('display','inline-block');app

$('#share_button').css('display','inline-block');
$('#share_button').on( 'click' , function()
{post

//canvas.toDataURL()這是獲取64位編碼 編碼

 

post('/?a=share',{'data':canvas.toDataURL()});url


} );
// service.weibo.com/share/share.php?title=鍋峯伔鍛婅瘔浣狅紝鍙鏄寳浜簰鑱旂綉鍏徃鐨凥R錛岄兘鍙互鍏嶈垂鍘葷珵楣? @jobdeer 棰嗘姳鏋曞摝錛佷笉鐢ㄦ娊濂栵紝1000涓姳鏋曞厛鍒板厛寰楋紝浣犱篃璧剁揣鍘婚鍚э紒&url=http%3A%2F%2Fwww.jobdeer.com%2Factivity.html&source=bookmark&pic=http://www.jobdeer.com/img/activity_weibo.png
/*
$('#share_button').attr( 'href' , 'http://service.weibo.com/share/share.php?title='+ encodeURIComponent('鏉ョ湅鐪嬫垜璁捐鐨凧obDeer') + '&url=' + encodeURIComponent('http://deerface.sinaapp.com') + '&pic=' + encodeURIComponent(canvas.toDataURL()) ) ;
$('#share_button').css('display','inline-block');
*/
}
});
}
}插件

相關文章
相關標籤/搜索