forked from jl-/ng-img-thumb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg-thumb.js
43 lines (42 loc) · 1.54 KB
/
img-thumb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
;
'use strict';
/*========
thanks to https://github.com/nervgh/angular-file-upload/blob/master/examples/image-preview/directives.js
================*/
angular.module('ngImgThumb',[])
.directive('imgThumb', ['$window',
function($window) {
var helper = {
support: !!$window.FileReader,
isFile: function(item) {
return angular.isObject(item) && item instanceof $window.File;
},
isImage: function(file) {
var type = '|' + file.type.slice(file.type.lastIndexOf('/') + 1) + '|';
return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
}
};
return {
restrict: 'E',
replace: true,
scope: {
file: '='
},
template: '<img class="thumbnail"/>',
link: function(scope, element, attrs) {
var file = scope.file.getNative();
if (!helper.support) return;
if (!helper.isFile(file)) return;
if (!helper.isImage(file)) return;
function loadImg(){
var reader = new FileReader();
reader.onload = function(e){
element[0].src = this.result;
};
reader.readAsDataURL(file);
}
loadImg();
}
};
}
]);