function ImageFileResize(file, maxWidth, maxHeight, callback) {
var Img = new Image;
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
Img.onload = function() {
if (Img.width>maxWidth || Img.height>maxHeight) {
var bili = Math.max(Img.width/maxWidth, Img.height/maxHeight);
canvas.width = Img.width/bili;
canvas.height = Img.height/bili;
}else{
canvas.width = Img.width;
canvas.height = Img.height;
}
ctx.drawImage(Img, 0, 0, Img.width, Img.height, 0, 0, canvas.width, canvas.height);
// $('body').append(canvas);
callback(canvas.toDataURL());
};
try{
Img.src = window.URL.createObjectURL(file);
}catch(err){
try{
Img.src = window.webkitURL.createObjectURL(file);
}catch(err){
alert(err.message);
}
}
}
$('.js-uploader').on('click', function () {
var $clickObj = $(this);
var $fileInput = $('<input type="file"/>');
$fileInput.on('change',function () {
$clickObj.text('正在上传...');
ImageFileResize($fileInput[0].files[0], 800, 800, function (dataUrl) {
$.ajax({
type: "POST",
url: "<?=$this->createMobileUrl('Upload')?>",
data: {imgDatUrl:dataUrl},
success : function (ret) {
$clickObj.prev().remove();
$clickObj.before("<img src=\"" + dataUrl + "\"/> ");
$clickObj.next().val(ret.path);
$clickObj.text('重新上传');
},
dataType : "json"
});
});
});
$fileInput.click();
});
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。