Use NativeScript Plugin nativescript-background-http To Send And Store Camera Pictures On PHP Server

I searched and searched and asked on slack and asked on the NS FaceBook group and everything I could think of to figure this out. I finally pieced it together and it’s working. So do you want to let your app users take a picture with their camera, or use one in their library for something and you need that photo stored on your web server? We let users take before/progress/after pics from using our device. We want to store these photos so we can see results ourselves and request use from our users for advertisements. So here’s how you do it.

nativescript-background-http

You need to make sure and import your camera ui and also this plugin. It’s extremely awesome and works great. You can get it by running

tns plugin add nativescript-background-http

EDIT: For some reason when I put this on an actual device it wouldn’t save the photos. When on the actual phone it has a problem with using knownFolders.currentApp() and a custom folder, so I change that line from

fs.knownFolders.currentApp().path+“/saved_images“;

to

fs.knownFolders.documents().path;

and it worked just fine.

After setting that up use the below code, or whatever parts of it you’d like, to make the NS function and push the upload to your server. A couple notes though, I use a folder called /saved_images in my /app project directory to store the files before sending. I’m not sure this is necessary but I wanted to resize to a maximum of 800px wide or height while retaining proportion. It is extremely important this directory is created if you use this filepath or your script will fail and you won’t know why because error logging here doesn’t do anything.

Also, I store a global configuration variable for my api’s URL, so I use url: config.apiUrl in my request options.

Saving The Image In PHP

So the config.apiUrl points directly to my php file (ex:server.com/apifile.php). In this file you need to access file_get_contents(‘php://input’) to access the streamed data coming in. This is how you work with forms submitted using content type “application/octet-stream” to get the file data. With this data you can simply save it however you’d like using file_put_contents. There’s other security concerns you will need to address that I’m not going over here as they are different all different applications of this process. But here’s a quick, simple line for saving the image to a /uploads/ directory:

I was reading an article today about the new JPEG compression algorithm developed by Dropbox called Leptin which saves 22% losslessly. “It compresses JPEG files at a rate of 5 megabytes per second and decodes them back to the original bits at 15 megabytes per second, securely, deterministically, and in under 24 megabytes of memory.”

It made me remember this article when I got to “Lepton is a fully streamable format, meaning the decompression can be applied to any file as that file is being transferred over the network. Hence, streaming overlaps the computational work of the decompression with the file transfer itself, hiding latency from the user.”

That would be a huge boon for mobile image uploads from cameras, and a prime suspect for developing a NativeScript.js adapter that has the potential to be hugely popular as well as very performance enhancing to applications that use it.

Thanks for the awesome tutorial! I’m having a bit of a beginner’s problem, I’m afraid. Neither if(picsaved), nor it’s else statement, are being executed. The file on my server is also not being reached. Any idea why this would be? The rest of it works wonderfully.

I’m basically wanting to be able to upload images from the camera, preferably accompanied by some other data, and/or in a predictable filename convention.