直接上代码

directive.js

app.directive('fileUploadersm', function () {
return {
restrict: 'E',
transclude: true,
template: '<input type="file" accept="images/*" id="files" >' + '<div class="containertsm"></div><button ng-click="upload()" class="btn btn-danger">确定上传</button><br/>' + '<ul><li ng-repeat="file in files"> - </li></ul>',
controller: function ($scope, $fileUpload) {
$scope.notReady = true;
$scope.addfood = function () {
$fileUpload.upload($scope.files);
};
},
link: function ($scope, $element) {
////////////
var tmp = $('<div class="resizer">' + '<div class="inner">' + '<img class="imgs">' + '<div class="frames"></div>' + '</div>' + '<button class="ok">✓</button>' + '</div>');
$.imageResizer = function () {
if (Uint8Array && HTMLCanvasElement && atob && Blob) {
}
else {
return false;
}
var resizer = tmp.clone();
resizer.image = resizer.find('img')[0];
resizer.frames = resizer.find('.frames');
resizer.okButton = resizer.find('button.ok');
resizer.frames.offset = {
top: 0,
left: 0
};
resizer.okButton.click(function () {
resizer.clipImage();
});
resizer.clipImage = function () {
var nh = this.image.naturalHeight, nw = this.image.naturalWidth, size = nw > nh ? nh : nw;
size = size > 150 ? 150 : size;
var canvas = $('<canvas class="tcanvas" width="' + size + '" height="' + size + '"></canvas>')[0],
ctx = canvas.getContext('2d'),
scale = nw / this.offset.width,
x = this.frames.offset.left * scale,
y = this.frames.offset.top * scale,
w = this.frames.offset.size * scale,
h = this.frames.offset.size * scale;
ctx.drawImage(this.image, x, y, w, h, 0, 0, size, size); var src = canvas.toDataURL();
this.canvas = canvas;
this.append(canvas);
this.addClass('uploading');
this.removeClass('have-img');
src = src.split(',')[1];
if (!src)
return this.doneCallback(null);
src = window.atob(src);
var ia = new Uint8Array(src.length);
for (var i = 0; i < src.length; i++) {
ia[i] = src.charCodeAt(i);
}
;
this.doneCallback(new Blob([ia], { type: "image/png" }));
Id = new Blob([ia], { type: "image/png" });//这里Id 存放的就是裁剪之后的相片你可以自定义全局参数
};
resizer.resize = function (file, done) {
this.reset();
this.doneCallback = done;
this.setFrameSize(0);
this.frames.css({
top: 0,
left: 0
});
var reader = new FileReader();
reader.onload = function () {
resizer.image.src = reader.result;
reader = null;
resizer.addClass('have-img');
resizer.setFrames();
};
reader.readAsDataURL(file);
};
resizer.reset = function () {
this.image.src = '';
this.removeClass('have-img');
this.removeClass('uploading');
this.find('canvas').detach();
};
resizer.setFrameSize = function (size) {
this.frames.offset.size = size;
return this.frames.css({
width: size + 'px',
height: size + 'px'
});
};
resizer.getDefaultSize = function () {
var width = this.find(".inner").width(), height = this.find(".inner").height();
this.offset = {
width: width,
height: height
};
console.log(this.offset);
return width > height ? height : width;
};
resizer.moveFrames = function (offset) {
var x = offset.x, y = offset.y, top = this.frames.offset.top, left = this.frames.offset.left, size = this.frames.offset.size, width = this.offset.width, height = this.offset.height;
if (x + size + left > width) {
x = width - size;
}
else {
x = x + left;
}
;
if (y + size + top > height) {
y = height - size;
}
else {
y = y + top;
}
;
x = x < 0 ? 0 : x;
y = y < 0 ? 0 : y;
this.frames.css({
top: y + 'px',
left: x + 'px'
});
this.frames.offset.top = y;
this.frames.offset.left = x;
};
(function () {
var time;
function setFrames() {
var size = resizer.getDefaultSize();
resizer.setFrameSize(size);
}
;
window.onresize = function () {
clearTimeout(time);
time = setTimeout(function () {
setFrames();
}, 1000);
};
resizer.setFrames = setFrames;
})();
(function () {
var lastPoint = null;
function getOffset(event) {
event = event.originalEvent;
var x, y;
if (event.touches) {
var touch = event.touches[0];
x = touch.clientX;
y = touch.clientY;
}
else {
x = event.clientX;
y = event.clientY;
}
if (!lastPoint) {
lastPoint = {
x: x,
y: y
};
}
;
var offset = {
x: x - lastPoint.x,
y: y - lastPoint.y
};
lastPoint = {
x: x,
y: y
};
return offset;
}
;
resizer.frames.on('touchstart mousedown', function (event) {
getOffset(event);
});
resizer.frames.on('touchmove mousemove', function (event) {
if (!lastPoint)
return;
var offset = getOffset(event);
resizer.moveFrames(offset);
});
resizer.frames.on('touchend mouseup', function (event) {
lastPoint = null;
});
})();
return resizer;
};
var resizer = $.imageResizer(), resizedImage;
if (!resizer) {
resizer = $("<p>Your browser doesn't support these feature:</p><ul><li>canvas</li><li>Blob</li><li>Uint8Array</li><li>FormData</li><li>atob</li></ul>");
}
;
$('.containertsm').append(resizer);
///////////////////
var fileInput = $element.find('input[type="file"]');
fileInput.bind('change', function (e) {
var file = this.files[0];
resizer.resize(file, function (file) {
resizedImage = file;
});
$scope.notReady = e.target.files.length == 0;
$scope.files = [];
for (i in e.target.files) {
//Only push if the type is object for some stupid-ass reason browsers like to include functions and other junk
if (typeof e.target.files[i] == 'object')
$scope.files.push(e.target.files[i]);
}
});
// var file=this.files[0];
// resizer.resize(file,function(file){
// resizedImage=file;
// });
}
};
});

  部分内容 请参照 html5 实现本地上传裁剪 http://www.webrube.com/jquery-html5-web_rube/4300

html 代码

<div class="col-sm-6">
<div>
<file-uploader></file-uploader>
</div>
</div>

  

最新文章

  1. jQuery学习之prop和attr的区别示例介绍
  2. HighChart报表之饼图
  3. 在PYTHON中使用StringIO的性能提升实测(更新list-join对比)
  4. 64位系统上使用PLSQL Dervloper解决方案
  5. canvas事件
  6. Interview----First single charactor
  7. 探讨一个新的两个进程间的通信和编程模型 (Windows)
  8. fancybox的使用
  9. Qt Quick App的两种启动模式
  10. 利用autoit自动关闭指定标题窗口
  11. kubernetes 条件需求
  12. sockaddr结构体
  13. 购买的wemall 6.0商城系统源码分享
  14. 利用HTML5判断用户是否正在浏览页面技巧
  15. 配置scrapy-splash+python爬取医院信息(利用了scrapy-splash)
  16. MySql优化子查询
  17. Rest Framework
  18. Xposed 在android 6.0上报couldn&#39;t load class,找不到xposed_init中配置的入口类
  19. [PHP]常量的一些特性
  20. SpringBank 开发日志 使用maven构建dubbo服务的可执行jar包

热门文章

  1. POJ 1609 Tiling Up Blocks
  2. HDU——1005Number Sequence(模版题 二维矩阵快速幂+操作符重载)
  3. 关于sudo dpkg-divert –local –rename –add /sbin/initctl导致的开机无图标解决方法
  4. net6:创建Membership对象数据源的代码
  5. watch watch watch the video! I got almost addicted. Oh what a fuck!!!!
  6. float 常见用法与问题--摘抄
  7. webRTC windows demo1(转)
  8. hdu 5461(分类讨论)
  9. 深入了解Entity Framework框架及访问数据的几种方式
  10. 2016-2017 ACM-ICPC, South Pacific Regional Contest (SPPC 16)