直接上代码

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>

  

AngularJs + html 5 实现 裁剪上传的更多相关文章

  1. 第三百九十节,Django+Xadmin打造上线标准的在线教育平台—Django+cropper插件头像裁剪上传

    第三百九十节,Django+Xadmin打造上线标准的在线教育平台—Django+cropper插件头像裁剪上传 实现原理 前台用cropper插件,将用户上传头像时裁剪图片的坐标和图片,传到逻辑处理 ...

  2. springMVC 头像裁剪上传并等比压

    第一次写头像裁剪上传,原本想着直接本地预览裁剪再上传,可是时间有限,jquery.jcrop貌似并没有对 假设是ie下图片预览效果是滤镜做的  做出对应处理,也没有时间去改;仅仅好将就一下先把图片上传 ...

  3. PHP+jQuery.photoClip.js支持手势的图片裁剪上传实例

    PHP+jQuery.photoClip.js支持手势的图片裁剪上传实例,在手机上双指捏合为缩放,双指旋转可根据旋转方向每次旋转90度,在电脑上鼠标滚轮为缩放,双击则顺时针旋转90度. 下面让我们来看 ...

  4. vue中使用cropperjs进行图片裁剪上传

    下面代码直接就可以复制使用了,但是需要在本地下个cropperjs,下载命令:npm install cropperjs --save-dev <template> <div id= ...

  5. 微信小程序实现图片裁剪上传(wepy)

    参考https://github.com/we-plugin/we-cropper,在wepy中实现,参考的具体例子是we-cropper/example/cutInside/ 项目上传图片时2:3的 ...

  6. jfinal头像裁剪上传服务器

    前端页面完整代码,复制可用,记得导入库文件 <!DOCTYPE html> <html lang="en"> <head> <title& ...

  7. mui+vue+photoclip做APP头像裁剪上传

    做APP由于项目需要,需要做用户头像上传的功能,头像上传包括拍照和相册选择图片进行上传,这里使用的技术是mui封装的plus,在进行图片裁剪的时候,使用的是photoclip来进行裁剪,由于个人在使用 ...

  8. angularjs结合plupload实现文件上传

    转载注明:(罗志强的博客) angularjs的指令directive非常好使,可以很方便的结合各种插件,实现很强大的功能. 今天用到了plupload,就拿它举例吧. 正常的plupload用法应该 ...

  9. 图片裁剪上传插件——jquery.photoClip.js

    想要裁剪图片上传: 需要依赖的的插件为: [jquery.photoClip.js] 插件[iscroll-zoom.js] 插件[hammer.js] 插件 [lrz.all.bundle.js] ...

随机推荐

  1. hdu6097[二分+解析几何] 2017多校6

    /*hdu6097[二分+解析几何] 2017多校6*/ #include <bits/stdc++.h> using namespace std; ; struct node{ doub ...

  2. 洛谷P3768 简单的数学题 【莫比乌斯反演 + 杜教筛】

    题目描述 求 \[\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} i*j*gcd(i,j) \pmod{p}\] \(n<=10^{10}\),\(p\) ...

  3. 刷题总结——work(ssoj)

    题目: 题目背景 SOURCE:NOIP2015-SHY-5 题目描述 假设现在离 noip 还有 m 天,有 n 个人要去参加比赛.他们每个人都有一个预定的训练量 r[i] ,所以每一天他们都抓紧时 ...

  4. Eclipse + Jersey 发布RESTful WebService(一)了解Maven和Jersey,创建一个WS项目(成功!)

    一.下文中需要的资源地址汇总 Maven Apache Maven网站 http://maven.apache.org/ Maven下载地址: http://maven.apache.org/down ...

  5. hdu 2888 二维RMQ

    Check Corners Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. math对象的方法

    方法 描述 abs(x) 返回数的绝对值. acos(x) 返回数的反余弦值. asin(x) 返回数的反正弦值. atan(x) 以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切 ...

  7. ping++微信渠道,第二次拉起不能进行支付返回订单号重复问题

    项目中用到了支付功能,采用的是ping++实现的,上线运行一年多都很正常,但是最近突然出现有买家反映说不能进行支付的情况 通过了解和沟通之后发现发现是重复拉起失败,然后我们对问题进行了排查. 测试过程 ...

  8. L1-2. 点赞【求多组数据中出现次数最多的】

    L1-2. 点赞 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 微博上有个“点赞”功能,你可以为你喜欢的博文点个赞表示支持.每 ...

  9. 2017-11-07-noip模拟题

    T1 数学老师的报复 矩阵快速幂模板,类似于菲波那切数列的矩阵 [1,1]*[A,1 B,0] #include <cstdio> #define LL long long inline ...

  10. UvaLive 4287 Proving Equivalences 强连通缩点

    原题链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...