Arm

unread,
Jun 5, 2015, 6:40:28 AM6/5/15
to mitappinv...@googlegroups.com
Hi guys!

Recently I've been desperately in search of a chunk of code that would help me to solve this problem - how to resize an image client side before sending it to the server. Server side resizing is possible and way easier, but do it in the client helps to decrease the transfer time between client and server if you want to store the image remotely and sometimes becomes even necessary, for example to avoid some annoying timeout error in MIT when the connection is too slow.

The method I've used here is resizing to fit in a bounding box (MaxWidth,MaxHeight), which consists in finding which is smaller between MaxWidth/ImageWidth or MaxHeight/ImageHeight and then multiplying image width and height by that number, called scale factor.

It has been difficult to understand how to save the resized image on the phone before posting the same to the server. The only way to do that - or at least, the only way I found - is by using a canvas and its SaveAs method. I wish I could have done it "in background" by simply adjusting canvas width and height to the new image dimensions, changing canvas backgroundImage to contain the image and saving it without showing the canvas at all. Thing is, when the canvas is not visible, no matter how many times you try to set its width and height properties, the values will not change until you make it visible for the short time it needs to acquire the new values. Therefore, a bit of trickery has been necessary: canvas becomes visible and as soon as it takes the correct values for width and height, we hide it again, unless of course you are ok with showing it on screen, but if you're not, I suggest you find a way to avoid this annoying show-and-hide process by positioning, for instance, the canvas at the bottom of the screen and/or using a progress dialog.

Here the screenshots with some further explanation:


Here the code of the script I've used to calculate the original image's width and height:

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Get Image Width and Height</title>

</head>


<body>

<script>

// get the string from the app

var webviewstr = window.AppInventor.getWebViewString();

// remove the prefix ('REQ|') from the string, leaving only the image path

var img_path = webviewstr.substr(4);

// create an image object

var img = new Image();

// define the function that will be called when the image has finished loading

img.onload = function() {

// send image width and height back to the app

window.AppInventor.setWebViewString('RES|' + this['width'] + ',' + this['height']);

}

// load the image

img.src = img_path;

</script>

</body>

</html>

Please find attached the .aia project.

Hope it helps!

Armando

ImageResizing.aia

Taifun

unread,
Jun 5, 2015, 9:56:37 AM6/5/15
to mitappinv...@googlegroups.com
very nice! thank you for the example!
keep up the good work!
Taifun

Abraham Getzler

unread,
Jun 5, 2015, 12:42:55 PM6/5/15
to mitappinv...@googlegroups.com
FAQ worthy!
ABG

Arm

unread,
Jun 7, 2015, 10:47:12 AM6/7/15
to mitappinv...@googlegroups.com
Just to be on the safe side, please try to set canvas BackgroundImage at some point before where it is now... I've experienced one time an unpleasant thick grey line at the bottom of the resized image file as if, while saving the canvas, the background image hadn't been set yet, at least not completely.
Setting the background before - not straight before calling canvas SaveAs method, but earlier - should be enough to avoid this unwanted possibility.
Reply all
Reply to author
Forward
0 new messages