Skip to content

Some tweaks #1

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

Open
wants to merge 3 commits 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
34 changes: 26 additions & 8 deletions public/javascripts/imageupload.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ angular.module('imageuploadDemo')
}

return resizeArea;
}
};

var resizeImage = function (origImage, options, callback) {
options.maxHeight = options.maxHeight || 300;
options.maxWidth = options.maxWidth || 250;
options.quality = options.quality || 0.7;

var canvas = getResizeArea();

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

Expand Down Expand Up @@ -62,16 +61,28 @@ angular.module('imageuploadDemo')
inputId: '@',
originalImage: '=',
resizeImage: '=',
showError: "@",
errorMessage: "@",
resizeMaxHeight: '@',
resizeMaxWidth: '@',
resizeMaxWidth: '@'
},
template: '<div>' +
'<input id="{{inputId}}" type="file">' +
'<div>' +
'<a id="removeButton" ng-show="resizeImage">x</a>' +
'<div ng-show="!showError">' +
'<img ng-show="resizeImage" ng-src="{{resizeImage.url}}" type="{{image.type}}">' +
'</div>' +
'<div style="color:#ef3e42;">' +
'<span ng-show="showError">{{errorMessage}}</span>' +
'</div>' +
'</div>',
link: function postLink(scope, element, attrs) {
element.children('#removeButton').bind('click', function() {
element.children('#' + scope.inputId).val('');
scope.$apply(function(){
scope.originalImage = scope.image = scope.resizeImage = null;
});
});
element.bind('change', function (evt) {
var image = scope.image = evt.target.files[0];

Expand All @@ -81,7 +92,15 @@ angular.module('imageuploadDemo')

// Only process image files.
if (!image.type.match('image.*')) {
scope.$apply(function() {
scope.showError = true;
scope.errorMessage = "It must be an image (it will be ignored).";
});
return;
}else{
scope.$apply(function() {
scope.showError = false;
});
}

var reader = new FileReader();
Expand All @@ -90,7 +109,6 @@ angular.module('imageuploadDemo')

//create Blob from loaded ArrayBuffer
var origImageBlob = new Blob([new Int8Array(e.target.result)], { type: image.type });

//attach url to origImageBlob
origImageBlob.url = URL.createObjectURL(origImageBlob);

Expand All @@ -101,7 +119,7 @@ angular.module('imageuploadDemo')
var origImage = new Image();
origImage.onload = function () {
resizeImage(origImage, {
maxHeight: scope.resizeMaxHeight,
maxHeight: scope.resizeMaxHeight,
maxWidth: scope.resizeMaxWidth,
quality: scope.resizeQuality
}, function (resizedImageBlob) {
Expand All @@ -111,7 +129,7 @@ angular.module('imageuploadDemo')
//attach image name to resizedImageBlob
resizedImageBlob.name = image.name;

scope.$apply(function () {
scope.$apply(function () {
scope.originalImage = origImageBlob;
scope.resizeImage = resizedImageBlob;
});
Expand All @@ -128,4 +146,4 @@ angular.module('imageuploadDemo')
}
};
}
);
);