Last active
October 27, 2015 21:26
-
-
Save abellion/d70a0e841953d56eee4e to your computer and use it in GitHub Desktop.
AngularJs service for caching images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
angular | |
.module('app') | |
.directive('img', img); | |
img.$inject = []; | |
function img() { | |
return ({ | |
restrict: 'E', | |
controller: imgController, | |
link: imgLink | |
}); | |
} | |
function imgLink(scope, element, attrs) { | |
var img = element[0]; | |
var load = scope.load(attrs['src'], 'blob', 100); | |
load.then(function(src) { | |
img['src'] = src; | |
}); | |
} | |
imgController.$inject = ['$scope', 'my_img']; | |
function imgController($scope, my_img) { | |
var self = $scope; | |
self.load = my_img.load; | |
} | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
angular | |
.module('app') | |
.factory('my_img', my_img); | |
my_img.$inject = ['$q', '$http']; | |
function my_img($q, $http) { | |
var self = this; | |
var _imgs = {}; | |
var _getDataType = function(src) { | |
var ext = src.replace(/^[a-z\/\\]*\./i, ''); | |
var hash = {'jpg': 'image/jpeg', 'png': 'image/png', 'jpeg': 'image/jpeg'}; | |
return (hash[ext]); | |
}; | |
var _getBase64Url = function(src, quality) { | |
var deferred = $q.defer(); | |
var img = new Image(); | |
img['src'] = src; | |
img.onload = function() { | |
var canvas = document.createElement('CANVAS'); | |
canvas.height = this.height; | |
canvas.width = this.width; | |
canvas.getContext('2d').drawImage(this, 0, 0); | |
deferred.resolve(canvas.toDataURL(_getDataType(src), quality)); | |
}; | |
return (deferred.promise); | |
}; | |
var _getBlobUrl = function(src, quality) { | |
var deferred = $q.defer(); | |
$http.get(src, { 'responseType': 'arraybuffer' }).then(function(result) { | |
var blob = new Blob([ result['data'] ], { 'type': _getDataType(src) }); | |
blob = blob.slice(0, blob.size * quality, _getDataType(src)); | |
var urlCreator = window.URL || window.webkitURL; | |
var imageUrl = urlCreator.createObjectURL( blob ); | |
deferred.resolve(imageUrl); | |
}); | |
return (deferred.promise); | |
}; | |
/* | |
** src : source image | |
** [encoding] : encoding type, b64 (base64) or blob -- Default blob | |
** [quality] : quality from 0 to 100 -- Default 100 | |
*/ | |
self.load = function(src, encoding, quality) { | |
if (!_getDataType(src)) { | |
return ($q.resolve(src)); | |
} | |
var hash = {'b64': _getBase64Url, 'blob': _getBlobUrl}; | |
var encodingFn = encoding && hash[encoding] || hash['blob']; | |
quality = quality && (quality / 100) || 1; | |
_imgs[src] = (!_imgs[src]) ? encodingFn.apply(null, [src, quality]) : _imgs[src]; | |
return (_imgs[src]); | |
}; | |
return (self); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment