Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make it minification friendly #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
271 changes: 133 additions & 138 deletions public/javascripts/imageupload.js
Original file line number Diff line number Diff line change
@@ -1,138 +1,133 @@
angular.module('imageupload', [])
.directive('image', function($q) {
'use strict'

var URL = window.URL || window.webkitURL;

var getResizeArea = function () {
var resizeAreaId = 'fileupload-resize-area';

var resizeArea = document.getElementById(resizeAreaId);

if (!resizeArea) {
resizeArea = document.createElement('canvas');
resizeArea.id = resizeAreaId;
resizeArea.style.visibility = 'hidden';
document.body.appendChild(resizeArea);
}

return resizeArea;
}

var resizeImage = function (origImage, options) {
var maxHeight = options.resizeMaxHeight || 300;
var maxWidth = options.resizeMaxWidth || 250;
var quality = options.resizeQuality || 0.7;
var type = options.resizeType || 'image/jpg';

var canvas = getResizeArea();

var height = origImage.height;
var width = origImage.width;

// calculate the width and height, constraining the proportions
if (width > height) {
if (width > maxWidth) {
height = Math.round(height *= maxWidth / width);
width = maxWidth;
}
} else {
if (height > maxHeight) {
width = Math.round(width *= maxHeight / height);
height = maxHeight;
}
}

canvas.width = width;
canvas.height = height;

//draw image on canvas
var ctx = canvas.getContext("2d");
ctx.drawImage(origImage, 0, 0, width, height);

// get the data from canvas as 70% jpg (or specified type).
return canvas.toDataURL(type, quality);
};

var createImage = function(url, callback) {
var image = new Image();
image.onload = function() {
callback(image);
};
image.src = url;
};

var fileToDataURL = function (file) {
var deferred = $q.defer();
var reader = new FileReader();
reader.onload = function (e) {
deferred.resolve(e.target.result);
};
reader.readAsDataURL(file);
return deferred.promise;
};


return {
restrict: 'A',
scope: {
image: '=',
resizeMaxHeight: '@?',
resizeMaxWidth: '@?',
resizeQuality: '@?',
resizeType: '@?',
},
link: function postLink(scope, element, attrs, ctrl) {

var doResizing = function(imageResult, callback) {
createImage(imageResult.url, function(image) {
var dataURL = resizeImage(image, scope);
imageResult.resized = {
dataURL: dataURL,
type: dataURL.match(/:(.+\/.+);/)[1],
};
callback(imageResult);
});
};

var applyScope = function(imageResult) {
scope.$apply(function() {
//console.log(imageResult);
if(attrs.multiple)
scope.image.push(imageResult);
else
scope.image = imageResult;
});
};


element.bind('change', function (evt) {
//when multiple always return an array of images
if(attrs.multiple)
scope.image = [];

var files = evt.target.files;
for(var i = 0; i < files.length; i++) {
//create a result object for each file in files
var imageResult = {
file: files[i],
url: URL.createObjectURL(files[i])
};

fileToDataURL(files[i]).then(function (dataURL) {
imageResult.dataURL = dataURL;
});

if(scope.resizeMaxHeight || scope.resizeMaxWidth) { //resize image
doResizing(imageResult, function(imageResult) {
applyScope(imageResult);
});
}
else { //no resizing
applyScope(imageResult);
}
}
});
}
};
});
'use strict'

function ImageUpload($q) {

var URL = window.URL || window.webkitURL;

var getResizeArea = function() {
var resizeAreaId = 'fileupload-resize-area';

var resizeArea = document.getElementById(resizeAreaId);

if (!resizeArea) {
resizeArea = document.createElement('canvas');
resizeArea.id = resizeAreaId;
resizeArea.style.visibility = 'hidden';
document.body.appendChild(resizeArea);
}

return resizeArea;
}

var resizeImage = function(origImage, options) {
var maxHeight = options.resizeMaxHeight || 300;
var maxWidth = options.resizeMaxWidth || 250;
var quality = options.resizeQuality || 0.7;
var type = options.resizeType || 'image/jpg';

var canvas = getResizeArea();

var height = origImage.height;
var width = origImage.width;

// calculate the width and height, constraining the proportions
if (width > height) {
if (width > maxWidth) {
height = Math.round(height *= maxWidth / width);
width = maxWidth;
}
} else {
if (height > maxHeight) {
width = Math.round(width *= maxHeight / height);
height = maxHeight;
}
}

canvas.width = width;
canvas.height = height;

// draw image on canvas
var ctx = canvas.getContext("2d");
ctx.drawImage(origImage, 0, 0, width, height);

// get the data from canvas as 70% jpg (or specified type).
return canvas.toDataURL(type, quality);
};

var createImage = function(url, callback) {
var image = new Image();
image.onload = function() {
callback(image);
};
image.src = url;
};

var fileToDataURL = function(file) {
var deferred = $q.defer();
var reader = new FileReader();
reader.onload = function(e) {
deferred.resolve(e.target.result);
};
reader.readAsDataURL(file);
return deferred.promise;
};

return {
restrict : 'A',
scope : {
image : '=',
resizeMaxHeight : '@?',
resizeMaxWidth : '@?',
resizeQuality : '@?',
resizeType : '@?', },
link : function postLink(scope, element, attrs, ctrl) {

var doResizing = function(imageResult, callback) {
createImage(imageResult.url, function(image) {
var dataURL = resizeImage(image, scope);
imageResult.resized = {
dataURL : dataURL,
type : dataURL.match(/:(.+\/.+);/)[1], };
callback(imageResult);
});
};

var applyScope = function(imageResult) {
scope.$apply(function() {
// console.log(imageResult);
if (attrs.multiple)
scope.image.push(imageResult);
else
scope.image = imageResult;
});
};

element.bind('change', function(evt) {
// when multiple always return an array of images
if (attrs.multiple)
scope.image = [];

var files = evt.target.files;
for (var i = 0; i < files.length; i++) {
// create a result object for each file in files
var imageResult = {
file : files[i],
url : URL.createObjectURL(files[i]) };

fileToDataURL(files[i]).then(function(dataURL) {
imageResult.dataURL = dataURL;
});

if (scope.resizeMaxHeight || scope.resizeMaxWidth) { // resize
// image
doResizing(imageResult, function(imageResult) {
applyScope(imageResult);
});
} else { // no resizing
applyScope(imageResult);
}
}
});
} };
}
angular.module('imageupload', []).directive('image', [ '$q', ImageUpload ]);