Uploading images using codea?

I have a website, I can download images from it easily. But say I want someone to be able to make an image in my app, and upload it to a part of my website that allows viewing of images etc. How would I do that?

My best guess is POST in the http parameter table

yeah, but you have to make a PHP program on your server side to handle them. Maybe @Toffer has done that for CC?

@Luatee, you would have a lot of trouble, mostly because you aren’t allowed to actually upload an image, you’d have to decode it into a string, which can cause a long freeze depending on the size of the image.

@Simeon, any chance of being able to send an image along http.request using POST and headers?

Sharing features off the list then =(( even 50000 pixels would take a long period of time, I hope they are still adding features to Codea 2.0 whilst testing progresses because I’ve seen some very useful features mentioned like yours @JakAttak

Are you sure it would take all that much time? Plain Lua loops can do well over a million iterations per second on a A7-based iPad.

If that’s not fast enough, maybe it’s possible to have a shader do the encoding? Hmmm, I think I read somewhere that shaders don’t work with non-frame-buffer image contexts, so that might not work.

@Luatee Toffer and I had talked about doing it for CC but thought it best to save the hosting space that TLL gave us. I believe toffer already has a way to do this, if not I bet he can figure it out. You’d have to encode and decode the image and sent it with POST data.

You could cheat and create a dropbox dev account and allow users to allow DB access. you could then upload it to dropbox, grab the image link and send that for proccessing on your server.

@Luatee it’s rather easy to read out the pixel values into a string and encode it with base64. I’m just not sure how to rebuild it on the server. Just googled it and it looks doable. I’ll see if I can get a demo up.

@Luatee - I’ve to check for that, but I remember to have done some test for sending raw data over PUT method from http.request and read / write the stream on server side. No need for data encoding.

@Briarfox, sorry, I haven’t see your post at time. Sure base64 is easy and not so heavy to encode decode on both client / server side.

I found it, a fast test on my localhost say it worked. If it can be a start point for you, here is the sources:

-- Read file data
-- according the file is in Documents folder
local filename = "MyFile.png"
local path = os.getenv("HOME").."/Documents/"..filename
local file = io.open(path,"rb")
local data = file:read("*all")
-- Request using PUT method
-- must have the right content type
-- filename.ext is passed throught GET param
headers = { ["Content-Type"] = "image/png" }
params = { method = "PUT", headers = headers, data = data }
-- replace domain_url

php server side script (upload.php)

// Naive file upload handling from PUT request
// where the filename and 'content-type' is grabed from the GET param
// Most of the status are returned under 400 in order to print error detail.

$protocol = $_SERVER['SERVER_PROTOCOL'];
$data = '';
if ($stream = fopen('php://input', 'r')) {
	while ($chunk = fread($stream, 1024))
		$data.= $chunk;
	$filename = isset($_GET['filename']) ? $_GET['filename'] : null;
	if ($filename) {
		if (file_put_contents($filename,$data)) {
			header($protocol . ' 201 Created', true, 201);
			exit('Upload of ' . $filename . ' complete');
		else {
			header($protocol . ' 500 Internal Server Error', true, 500);
			exit('Error while writing ' . $filename);
	else {
		header($protocol . ' 206 Partial Content', true, 206);
		exit('Filename is missing');

header($protocol . '204 No Content', true, 204);
exit('No stream found');


@toffer you beat me to it :slight_smile: Your way is much nicer. I sent the pixel info in a POST and rebuilt the image server side then saved it on the server. I never considered using fileIO :slight_smile: You are the Jedi master.

@toffer - beautiful! =D>

@toffer amazing work thank you. Nothing is impossible I see! Thanks to all for your input.

@toffer, brilliant. 8->

@toffer change your user name to PHP-boss!