Image upload to server from album/camera

Hi
I have a little problem related to uploading an image to server taken with the camera or from the photo album. I am using the following code to upload the image to my server: But with this code my server only recive the table value of the image like so: table: 0x11791daf0, how can I get it to upload the image instead of the value?

Thank you in advance :slight_smile:

[code]local function monitorMem(event)
collectgarbage(“collect”)

print( “\nMemUsage: " … (collectgarbage(“count”)/1000) … " MB”)
print("Texture Usage " … system.getInfo( “textureMemoryUsed” ) / 1000000)

return true
end

Runtime:addEventListener(“enterFrame”, monitorMem)

local image

local mime = require “mime”

local bkgd = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
bkgd:setFillColor( 0, 0, 0 )

local myRoundedRect = display.newRoundedRect(10, 50, 80, 50, 12)
myRoundedRect.strokeWidth = 3
myRoundedRect:setFillColor(140, 140, 140)
myRoundedRect:setStrokeColor(180, 180, 180)

local sessionComplete = function(event)
image = event.target

print( "Camera ", ( image and “returned an image” ) or “session was cancelled” )
print( "event name: " … event.name )
print( "target: " … tostring( image ) )

if image then
– center image on screen

image.x = display.contentWidth/2
image.y = 59
local w = image.width
local h = image.height
image.xScale = 0.3
image.yScale = 0.3
print( “w,h = “… w …”,” … h )
end
end

local listener = function( event )
if media.hasSource( media.Camera ) then
media.show( media.Camera, sessionComplete )
else
native.showAlert(“Corona”, “Camera not found.”)
end
return true
end
myRoundedRect:addEventListener( “tap”, listener )
local myRoundedRect1 = display.newRoundedRect(10, 400, 150, 50, 12)
myRoundedRect1.strokeWidth = 3
myRoundedRect1:setFillColor(140, 140, 140)
myRoundedRect1:setStrokeColor(180, 180, 180)

local Name = “Imagename”
function uploadBinary ( filename, url, onComplete )

– local path = system.pathForFile( filename )
– local fileHandle = io.open( path, “rb” )
– if fileHandle then

if image then

local params = {
body = “image_file=” … mime.b64(tostring( image )) … “&image_filename=”…Name
}

– io.close( fileHandle )

local function networkListener ( event )
if (onComplete) then
onComplete(event);
end
return true;
end

network.request( url, “POST”, networkListener, params)
end
end

local function networkListener( event )
if ( event.isError ) then
print( “Network error!”)
else
– print ( "RESPONSE: " … event.response)
print (“Working”)
end
end

local function Upload ()

uploadBinary ( image, “http://www.test1.bugs3.com/Corona.php”, networkListener)
end

myRoundedRect1:addEventListener( “tap”, Upload )

[/code]

and the PHP script on my server:

[code]<?php
$image_file = fopen($_POST[‘image_filename’], ‘wb’);

$encodedData = str_replace(’ ‘,’+’,$_POST[‘image_file’]);
$decocedData = base64_decode($encodedData);

fwrite($image_file, $decocedData);
fclose($image_file);

?>
[/code]
[import]uid: 36366 topic_id: 31704 reply_id: 331704[/import]

Bump, do I need to write the image to a file before I upload, with the filewrite code?

Thank you for the help in advance :smiley: [import]uid: 36366 topic_id: 31704 reply_id: 126699[/import]

According to the docs media.show() returns a Display Object, which is a table. You need to turn that Display Object into an actual picture. Looks like the easiest way to do that is with the optional file parameter:
[lua]local camOptions = { baseDir=system.TemporaryDirectory, filename=“myPicFile”, type=“image” }
media.show( media.Camera, sessionComplete, camOptions )[/lua]
When the picture is taken it will be saved to that file, then when you go to upload just grab that myPicFile and send it to the server.

Jay

PS - This is theoretical – while I’ve played with the camera and done lots of uploading, I haven’t done the two together. [import]uid: 9440 topic_id: 31704 reply_id: 126700[/import]

Hi Jay

Thank you very much for your answer. I am a little confused related to where I need to place the code you have posted. Is this correct?

local listener = function( event )  
 if media.hasSource( media.Camera ) then  
 local camOptions = { baseDir=system.TemporaryDirectory, filename="Myfile", type="image" }  
media.show( media.Camera, sessionComplete, camOptions )  
  
 else  
 native.showAlert("Corona", "Camera not found.")  
 end  
 return true  
end  

And also what information do I need to put into my upload code in order to upload this image, I thought something like this, but it dosen’t seem to work.

[code]function uploadBinary ( filename, url, onComplete )

local path = system.pathForFile( “Myfile”, system.TemporaryDirectory) – Do I need an extension like jpg, png after “Myfile”?
local fileHandle = io.open( path, “rb” )
if fileHandle then

local params = {
body = “image_file=” … mime.b64(fileHandle:read( “*a” )) … “&image_filename=”…Name
}

io.close( fileHandle )

local function networkListener ( event )
if (onComplete) then
onComplete(event);
end
return true;
end

network.request( url, “POST”, networkListener, params)
end
end

local function networkListener( event )
if ( event.isError ) then
print( “Network error!”)
else
– print ( "RESPONSE: " … event.response)
print (“Working”)
end
end

local function Upload ()

uploadBinary ( Myfile, “http://www.test1.bugs3.com/Corona.php”, networkListener)
end
[/code]
Thank you in advance for your help, I really appreciate it :slight_smile:
[import]uid: 36366 topic_id: 31704 reply_id: 126704[/import]

I followed this -

http://developer.coronalabs.com/code/how-upload-image-server-multipartform-data

Wasn’t images from a camera though.

Dave [import]uid: 117617 topic_id: 31704 reply_id: 126715[/import]

Hey
Thank you, I couldn’t get the method you have linked to work, so I used the following instead: http://developer.coronalabs.com/code/upload-binary-corona-php-script
I just can’t get the code J. A. Whye/Jay, posted above to work and I am a little confused which parameters I have to pass to my upload script?
I really appreciate any type of help, thank you in advance :slight_smile: (my code is posted above)

Best regards
Me :smiley: [import]uid: 36366 topic_id: 31704 reply_id: 126807[/import]

Sorry for double post :frowning: I have found the solution to my problem: See this topic: http://developer.coronalabs.com/forum/2012/09/23/using-mediashow-file-option#comment-126882

Mark this topic as resolved [import]uid: 36366 topic_id: 31704 reply_id: 126861[/import]

Bump, do I need to write the image to a file before I upload, with the filewrite code?

Thank you for the help in advance :smiley: [import]uid: 36366 topic_id: 31704 reply_id: 126699[/import]

According to the docs media.show() returns a Display Object, which is a table. You need to turn that Display Object into an actual picture. Looks like the easiest way to do that is with the optional file parameter:
[lua]local camOptions = { baseDir=system.TemporaryDirectory, filename=“myPicFile”, type=“image” }
media.show( media.Camera, sessionComplete, camOptions )[/lua]
When the picture is taken it will be saved to that file, then when you go to upload just grab that myPicFile and send it to the server.

Jay

PS - This is theoretical – while I’ve played with the camera and done lots of uploading, I haven’t done the two together. [import]uid: 9440 topic_id: 31704 reply_id: 126700[/import]

Hi Jay

Thank you very much for your answer. I am a little confused related to where I need to place the code you have posted. Is this correct?

local listener = function( event )  
 if media.hasSource( media.Camera ) then  
 local camOptions = { baseDir=system.TemporaryDirectory, filename="Myfile", type="image" }  
media.show( media.Camera, sessionComplete, camOptions )  
  
 else  
 native.showAlert("Corona", "Camera not found.")  
 end  
 return true  
end  

And also what information do I need to put into my upload code in order to upload this image, I thought something like this, but it dosen’t seem to work.

[code]function uploadBinary ( filename, url, onComplete )

local path = system.pathForFile( “Myfile”, system.TemporaryDirectory) – Do I need an extension like jpg, png after “Myfile”?
local fileHandle = io.open( path, “rb” )
if fileHandle then

local params = {
body = “image_file=” … mime.b64(fileHandle:read( “*a” )) … “&image_filename=”…Name
}

io.close( fileHandle )

local function networkListener ( event )
if (onComplete) then
onComplete(event);
end
return true;
end

network.request( url, “POST”, networkListener, params)
end
end

local function networkListener( event )
if ( event.isError ) then
print( “Network error!”)
else
– print ( "RESPONSE: " … event.response)
print (“Working”)
end
end

local function Upload ()

uploadBinary ( Myfile, “http://www.test1.bugs3.com/Corona.php”, networkListener)
end
[/code]
Thank you in advance for your help, I really appreciate it :slight_smile:
[import]uid: 36366 topic_id: 31704 reply_id: 126704[/import]

I followed this -

http://developer.coronalabs.com/code/how-upload-image-server-multipartform-data

Wasn’t images from a camera though.

Dave [import]uid: 117617 topic_id: 31704 reply_id: 126715[/import]

Hey
Thank you, I couldn’t get the method you have linked to work, so I used the following instead: http://developer.coronalabs.com/code/upload-binary-corona-php-script
I just can’t get the code J. A. Whye/Jay, posted above to work and I am a little confused which parameters I have to pass to my upload script?
I really appreciate any type of help, thank you in advance :slight_smile: (my code is posted above)

Best regards
Me :smiley: [import]uid: 36366 topic_id: 31704 reply_id: 126807[/import]

Sorry for double post :frowning: I have found the solution to my problem: See this topic: http://developer.coronalabs.com/forum/2012/09/23/using-mediashow-file-option#comment-126882

Mark this topic as resolved [import]uid: 36366 topic_id: 31704 reply_id: 126861[/import]