Decrease Image Size and/or Resolution before uploading

Hello everyone, I’m really stuck on this issue and can use help. Is there anyway to decrease the length of the base64 string being generated by the below code? Possibly by decreasing the image size or resolution? The variable here I’m sending is within the capturePhoto function is b64data and is a HUGE string and keeps crashing my server. The photos submitted to the server do not need to be of high resolution and can be as small as 500x500 pixels. Any advice is appreciated.

My JavaScript:

var Observable = require("FuseJS/Observable");
let ImageTools = require("FuseJS/ImageTools");
var FileSystem = require("FuseJS/FileSystem");
var Base64 = require("FuseJS/Base64");
var Camera = _camera;
var b64data;
var arrayBuff;

var captureMode = Observable();
var flashMode = Observable();

function getCameraInfo() {
	Camera.getCameraInfo()

		.then(function(info) {
			console.log("captureMode: " + info[Camera.INFO_CAPTURE_MODE]);
			console.log("flashMode: " + info[Camera.INFO_FLASH_MODE]);
			console.log("cameraFacing: " + info[Camera.INFO_CAMERA_FACING]);
			console.log("supportedFlashModes: " + info[Camera.INFO_SUPPORTED_FLASH_MODES].join());

			captureMode.value = info[Camera.INFO_CAPTURE_MODE];
			flashMode.value = info[Camera.INFO_FLASH_MODE];

			if (Camera.INFO_PHOTO_RESOLUTIONS in info) {
				var availableResolutions = info[Camera.INFO_PHOTO_RESOLUTIONS];
				availableResolutions.forEach(function(e) {
					console.log(e.width + "x" + e.height);
				});
				photoResolution = availableResolutions[Math.floor(availableResolutions.length * 0.4)];

				var options = {};
				options[Camera.OPTION_PHOTO_RESOLUTION] = photoResolution;

				Camera.setPhotoOptions(options)
					.then(function() {
						console.log("New photo options set: " + JSON.stringify(options));
					})
					.catch(function(error) {
						console.log("Failed to set photo options: " + error);
					});
			}
		})
		.catch(function(err) {
			console.log("Failed to get camera info: " + err);
		});
}
getCameraInfo();

function nextFlashMode() {
	if (flashMode.value == Camera.FLASH_MODE_AUTO) return Camera.FLASH_MODE_ON;
	else if (flashMode.value == Camera.FLASH_MODE_ON) return Camera.FLASH_MODE_OFF;
	else if (flashMode.value == Camera.FLASH_MODE_OFF) return Camera.FLASH_MODE_AUTO;
	else throw "Invalid flash mode";
}

function setCaptureMode(cm) {
	Camera.setCaptureMode(cm)
		.then(function(mode) {
			captureMode.value = mode;
			console.log("Capture mode set to: " + mode);
		})
		.catch(function(err) {
			console.log("Failed to set capture mode: " + err);
		});
}

function capturePhoto() {
	Camera.capturePhoto()
		.then(function (photo) {
			photo.save()
				.then(function(filePath) {
					console.log("Photo saved to: " + filePath);

							

					arrayBuff = FileSystem.readBufferFromFileSync(filePath);
					b64data = Base64.encodeBuffer(arrayBuff); // send this to the backend
					photo.release();
				})
				.catch(function(error) {
					console.log("Failed to save photo: " + error);
					photo.release();
				});
		})
		.catch(function (error) {
			console.log("Failed to capture photo: " + error);
		});
}

var isRecording = Observable(false);
var recordingSession = null;

function startRecording() {
	isRecording.value = true;
	Camera.startRecording()
		.then(function (session) {
			recordingSession = session;
		})
		.catch(function (error) {
			console.log("Failed to start recording: " + error);
			isRecording.value = false;
		});
}

function stopRecording() {
	isRecording.value = false;
	recordingSession.stop()
		.then(function (recording) {
			router.push("VideoPage", recording.filePath());
		})
		.catch(function (error) {
			console.log("Failed to stop recording: " + error);
		});
	recordingSession = null;
}

var cameraBack = true;
function flipCameraFacing() {
	var front = Camera.CAMERA_FACING_FRONT;
	var back = Camera.CAMERA_FACING_BACK;
	Camera.setCameraFacing(cameraBack ? front : back)
		.then(function (newFacing) {
			cameraBack = newFacing == back;
			getCameraInfo();
			console.log("Camera facing set to: " + (newFacing == back ? "back" : "front"));
		})
		.catch(function (err) {
			console.log("Failed to set camera facing: " + err);
		});
}

function changeFlashMode() {
	Camera.setFlashMode(nextFlashMode())
		.then(function(newFlashMode) {
			flashMode.value = newFlashMode;
			console.log("Flash mode set to: " + flashMode.value);
		})
		.catch(function(err) {
			console.log("Failed to set flash mode: " + err);
		});
}

var name = Observable();
var email = Observable();
var market = Observable();

module.exports = {
	name: name, 
	email: email,
	market: market,
	b64data: b64data,
	submitPhoto: submitPhoto,
	captureMode: captureMode,
	setCaptureModePhoto: function () { setCaptureMode(Camera.CAPTURE_MODE_PHOTO); },
	setCaptureModeVideo: function () { setCaptureMode(Camera.CAPTURE_MODE_VIDEO); },
	capturePhoto: capturePhoto,
	startRecording: startRecording,
	stopRecording: stopRecording,
	isRecording: isRecording,
	flipCameraFacing: flipCameraFacing,
	flashMode: flashMode,
	changeFlashMode: changeFlashMode,
}

function submitPhoto(){	


	console.log("name: "+name);
	console.log("email: "+email);
	console.log("market: "+market);

	var data ='name='+name+'&email='+email+'&market='+market+'&picture='+b64data;


	fetch('http://fanbeauties.com/app/submit-photo.php?pass=MY_PASS', { 
	method: "POST", 
	headers: {
      "Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
    },
    body: data
	 });


};

I think you should probably just resize the image before you even start the upload, take a look at ImageTools: https://www.fusetools.com/docs/fuse/imagetools/imagetools/resizeimageinterface_95a738ba

I am using the below code now attempting to do that with ImageTools but my app keeps crashing.

function capturePhoto() {
	Camera.capturePhoto()
		.then(function (photo) {
			photo.save()
				.then(function(filePath) {
					 var options = {
			    mode: ImageTools.IGNORE_ASPECT,
			    desiredWidth: 320, //The desired width in pixels
			    desiredHeight: 240 //The desired height in pixels
			};

			ImageTools.resize(photo, options)
					console.log("Photo saved to: " + filePath);
					var arrayBuff = FileSystem.readBufferFromFileSync(filePath);
					b64data = Base64.encodeBuffer(arrayBuff); // send this to the backend
					photo.release();
				})
				.catch(function(error) {
					console.log("Failed to save photo: " + error);
					photo.release();
				});
		})
		.catch(function (error) {
			console.log("Failed to capture photo: " + error);
		});
}