Skip to content

added stepwise image downsampling to improve the resulting image quality #22

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 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
23 changes: 20 additions & 3 deletions public/javascripts/imageupload.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ angular.module('imageupload', [])

var canvas = getResizeArea();

var height = origImage.height;
var width = origImage.width;
var oheight = origImage.height;
var owidth = origImage.width;
var height = oheight;
var width = owidth;

// calculate the width and height, constraining the proportions
if (width > height) {
Expand All @@ -43,12 +45,27 @@ angular.module('imageupload', [])
}
}

var img = origImage;
// introduce downsampling steps if appropriate
if(oheight / height > 2) {
img = document.createElement('canvas');
var tctx = img.getContext("2d");
img.width = owidth;
img.height = oheight;
tctx.drawImage(origImage, 0, 0, owidth, oheight, 0, 0, owidth/2, oheight/2);
oheight /= 2; owidth /= 2;
while(oheight / height > 2) {
tctx.drawImage(img, 0, 0, owidth, oheight, 0, 0, owidth/2, oheight/2);
oheight /= 2; owidth /= 2;
}
}

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

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

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