pyc-website

main website for pyc inc.

git clone https://9o.is/git/pyc-website.git

ngThumb.js

(1961B)


      1 /**
      2  * ngThumb is an angular-file-upload directive to preview uploaded images.
      3  * https://github.com/nervgh/angular-file-upload/blob/master/examples/image-preview/directives.js
      4  */
      5 angular.module("ngThumb", ['angularFileUpload'])
      6     .directive('ngThumb', ['$window', function($window) {
      7         var helper = {
      8             support: !!($window.FileReader && $window.CanvasRenderingContext2D),
      9             isFile: function(item) {
     10                 return angular.isObject(item) && item instanceof $window.File;
     11             },
     12             isImage: function(file) {
     13                 var type = '|' + file.type.slice(file.type.lastIndexOf('/') + 1) + '|';
     14                 return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
     15             }
     16         };
     17 
     18         return {
     19             restrict: 'A',
     20             template: '<canvas/>',
     21             link: function(scope, element, attributes) {
     22 
     23                 function onLoadFile(event) {
     24                     var img = new Image();
     25                     img.onload = onLoadImage;
     26                     img.src = event.target.result;
     27                 }
     28 
     29                 function onLoadImage() {
     30                     var width = params.width || this.width / this.height * params.height;
     31                     var height = params.height || this.height / this.width * params.width;
     32                     canvas.attr({ width: width, height: height });
     33                     canvas[0].getContext('2d').drawImage(this, 0, 0, width, height);
     34                 }
     35                 
     36                 if (!helper.support) { return; }
     37 
     38                 var params = scope.$eval(attributes.ngThumb);
     39 
     40                 if (!helper.isFile(params.file)) { return; }
     41                 if (!helper.isImage(params.file)) { return; }
     42 
     43                 var canvas = element.find('canvas');
     44                 var reader = new FileReader();
     45 
     46                 reader.onload = onLoadFile;
     47                 reader.readAsDataURL(params.file);
     48             }
     49         };
     50     }]);