'use strict'; angular .module('app') // Angular File Upload module does not include this directive // Only for example /** * The ng-thumb directive * @author: nerv * @version: 0.1.2, 2014-01-09 */ .directive('ngThumb', ['$window', function ($window) { var helper = { support: !!($window.FileReader && $window.CanvasRenderingContext2D), isFile: function (item) { return angular.isObject(item) && item instanceof $window.File; }, isAvailableExtension: function (file) { var type = '|' + file.type.slice(file.type.lastIndexOf('/') + 1) + '|'; return '|jpg|jpeg|png|gif|pdf|'.indexOf(type) !== -1; } }; var signatures = { "application/pdf": "pdf", "image/gif": "gif", "image/jpeg": "jpeg", "image/jpg": "jpg", "image/png": "png" }; function detectMimeType(b64) { for (var s in signatures) { if (b64.includes(s)) { return signatures[s]; } } } return { restrict: 'A', template: '
', link: function (scope, element, attributes) { if (!helper.support) return; var params = scope.$eval(attributes.ngThumb); if (!helper.isFile(params.file)) return; if (!helper.isAvailableExtension(params.file)) return; var canvas = element.find('canvas'); var div = element.find('div'); var reader = new FileReader(); reader.onload = onLoadFile; reader.readAsDataURL(params.file); function onLoadFile(event) { var img = new Image(); img.onload = onLoadImage; if(detectMimeType(event.target.result) == "pdf"){ img.src = "images/pdf-document-icon.svg"; }else{ img.src = event.target.result; } div[0].textContent = params.file.name; } function onLoadImage() { var width = params.width || this.width / this.height * params.height; var height = params.height || this.height / this.width * params.width; canvas.attr({ width: width, height: height }); canvas[0].getContext('2d').drawImage(this, 0, 0, width, height); } } }; }]);