Skip to content

Commit

Permalink
convert images to jpeg, gif, png
Browse files Browse the repository at this point in the history
  • Loading branch information
jwoo12 committed Mar 29, 2016
1 parent 0a08604 commit 0bda412
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 37 deletions.
57 changes: 31 additions & 26 deletions app/display/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,6 @@ angular.module("myApp.display", ["ngRoute", "LocalStorageModule", "myApp.display
usernameFromStorage = getItem(storageKey);
}

function parseImageResult (result){
// var photo = result.slice(8, result.length-2);
var photo = result.slice(7, result.length-2);

//console.log(photo);
return photo;
};

function parseThumbResult (result){
var thumb = result.slice(8, result.length-2);
return thumb;
};

$scope.editPhoto = function(){
displayHandler.curr_image = currentImage;
$location.url("/edit");
Expand All @@ -53,22 +40,42 @@ angular.module("myApp.display", ["ngRoute", "LocalStorageModule", "myApp.display
currentImage = selectedImage;
};


getImageSrc = function (photoBase64) {
var gifString = "R0lGOD", //The base 64 representation of a Gif start with this string
pngString = "iVBORw",
photoString = photoBase64.substring(7,13),
formattedPhoto,
imageSrc;

if(photoString == gifString || photoString == pngString){
//photo is a gif
formattedPhoto = photoBase64.slice(7, photoBase64.length-2)
imageSrc = "data:image/gif;base64," + formattedPhoto;
}else {
//photo is a jpeg
formattedPhoto = photoBase64.slice(8, photoBase64.length-2)
imageSrc = "data:image/jpeg;base64," + formattedPhoto;
}

return imageSrc;

};


// Gets the gallery for the user's uploaded images
displayHandler.getPictures(usernameFromStorage, function(result){
var index = 0;
if (result.data.images) {
for (index; index < result.data.images.length; index++){
var image = new Image(),
photoString = result.data.images[index],
photo = parseImageResult(photoString);
photoString = result.data.images[index];
var imageThumb = new Image(),
thumbString = result.data.thumbs[index],
thumb = parseThumbResult(thumbString);
thumbString = result.data.thumbs[index];

// image.src = "data:image/jpeg;base64," + photo;
image.src = "data:image/png;base64," + photo;
image.src = getImageSrc(photoString);
imageThumb.src = getImageSrc(thumbString);

imageThumb.src = "data:image/png;base64," + thumb;
$scope.myPictures.push({src: image.src,
id: result.data.rows[index][0],
owner: result.data.rows[index][1],
Expand All @@ -88,14 +95,12 @@ angular.module("myApp.display", ["ngRoute", "LocalStorageModule", "myApp.display
if (result.data.images) {
for (index; index < result.data.images.length; index++){
var image = new Image(),
photoString = result.data.images[index],
photo = parseImageResult(photoString);
photoString = result.data.images[index];
var imageThumb = new Image(),
thumbString = result.data.thumbs[index],
thumb = parseThumbResult(thumbString);
thumbString = result.data.thumbs[index];

image.src = "data:image/png;base64," + photo;
imageThumb.src = "data:image/png;base64," + thumb;
image.src = getImageSrc(photoString);
imageThumb.src = getImageSrc(thumbString);
// $scope.groupPictures.push({thumb: image.src, img: image.src, description: result.data.rows[index][3]})
$scope.groupPictures.push({src: image.src,
id: result.data.rows[index][0],
Expand Down
32 changes: 22 additions & 10 deletions app/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ angular.module("myApp.search", ["ngRoute", "LocalStorageModule", "myApp.search.s
return dateString
};

function parseImageResult (result){
// var photo = result.slice(8, result.length-2);
var photo = result.slice(7, result.length-2);

//console.log(photo);
return photo;
};

displayResults = function (photoResults, rowResults) {
var image = new Image(),
photoString,
Expand All @@ -59,9 +51,8 @@ angular.module("myApp.search", ["ngRoute", "LocalStorageModule", "myApp.search.s

for(var i = 0; i < photoResults.length; i++){
photoString = photoResults[i];
photo = parseImageResult(photoString);

image.src = "data:image/png;base64," + photo;
image.src = getImageSrc(photoString);
$scope.searchResults.push({src: image.src,
id: rowResults[i][0],
owner: rowResults[i][1],
Expand All @@ -76,6 +67,27 @@ angular.module("myApp.search", ["ngRoute", "LocalStorageModule", "myApp.search.s

};

getImageSrc = function (photoBase64) {
var gifString = "R0lGOD", //The base 64 representation of a Gif start with this string
pngString = "iVBORw",
photoString = photoBase64.substring(7,13),
formattedPhoto,
imageSrc;

if(photoString == gifString || photoString == pngString){
//photo is a gif
formattedPhoto = photoBase64.slice(7, photoBase64.length-2)
imageSrc = "data:image/gif;base64," + formattedPhoto;
}else {
//photo is a jpeg
formattedPhoto = photoBase64.slice(8, photoBase64.length-2)
imageSrc = "data:image/jpeg;base64," + formattedPhoto;
}

return imageSrc;

};



// Search function after the SEARCH button is pressed
Expand Down
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ getImages = function (row, index) {
// When the stream closes, resolce the promsie
lob.on("close", function (chunk) {

console.log("CLOSE PHOTO + RESOLVE");
// console.log("CLOSE PHOTO + RESOLVE");

// Fulfill promise
lobLoadingPhoto.resolve();
Expand Down

0 comments on commit 0bda412

Please sign in to comment.