Actionscript & PHP Development

Building apps using Adobe Air, Actionscript 3 and PHP

By
Warning: file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?latlng=,&sensor=false): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /home1/foncused/public_html/dev/wp-content/plugins/google-seo-author-snippets/microdata_form.php on line 228

Warning: file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?latlng=,&sensor=false): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /home1/foncused/public_html/dev/wp-content/plugins/google-seo-author-snippets/microdata_form.php on line 228
Richard Mackenzie

Uploading Camera UI bitmap data to your server with PHP

Air iosOnce you have obtained your bitmap data we use a combination of Actionscript and PHP to convert the bitmap data to a jpeg and upload it to you a web server. We do this in 2 main steps, first step is uploading the image to the server then the second step is posting information back to our application.

Once we have our bitmap data from the Camera UI, we can then convert that to a jpeg and upload it. First step is declaring some variables.

var imgBA:ByteArray;
var jpgEncoder:JPGEncoder;

imgBA = jpgEncoder.encode(smallBMD);
jpgEncoder = new JPGEncoder(70);

beginupload();
									

Basically we take our jpeg which was converted from a bitmap data file, and post it though to our PHP file.

function beginupload()
               {
  feedbackText.text = "upload image start";
   
    var sendHeader:URLRequestHeader = new URLRequestHeader("Content-type","application/octet-stream");
 
    var sendReq:URLRequest = new URLRequest("storeimg.php");
    sendReq.requestHeaders.push(sendHeader);
    sendReq.method = URLRequestMethod.POST;
    sendReq.data = imgBA;
 
    var sendLoader:URLLoader;
    sendLoader = new URLLoader();
    sendLoader.addEventListener(Event.COMPLETE, imageSentHandler);
    sendLoader.load(sendReq);
  
  feedbackText.text = "image uploading...";

} 


									

Now the above code calls a PHP file which basically takes the image that was posted, creates a unique filename name using date and time and then places it on the server.

<?php
if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) {
    $uniqueStamp = date(U);
    $filename = $uniqueStamp.".jpg";
    $fp = fopen( $filename,"wb");
    fwrite( $fp, $GLOBALS[ 'HTTP_RAW_POST_DATA' ] );
    fclose( $fp );
 
    echo "filename=".$filename."&base=".$_SERVER["HTTP_HOST"].dirname($_SERVER["PHP_SELF"]);
}
?>
									

When the image is copied it then echos back the unique filename of the uploaded image to our application.

function imageSentHandler(event:Event):void{
  
  feedbackText.text = "image uploaded!";

    var dataStr:String = event.currentTarget.data.toString();
    var resultVars:URLVariables = new URLVariables();
    resultVars.decode(dataStr);
   
    var imagePath = "http://" + resultVars.base + "/" + resultVars.filename;
    feedbackText.text = "Uploaded to: " + imagePath;
  
  variables.photo_id = resultVars.filename;

var request:URLRequest = new URLRequest("submit_photo.php");
request.method = URLRequestMethod.POST;
request.data = variables;

var loaderphoto:URLLoader = new URLLoader();
loaderphoto.dataFormat = URLLoaderDataFormat.TEXT;

try{
    loaderphoto.load(request);
  feedbackText.text ="info submited to db, Uploaded to: " + imagePath;
}
catch (error:Error) {
    feedbackText.text ="Unable to load URL submit_photo.php";
}
}
									

This part of our code listed out for feedback from the PHP file, and will tell us if there was an error or not, also gives us the filename in our application to use.

You will see a second PHP file here, this file I use to take the unique file name and write some information to a MYSQL database for my application such as where the photo was taken, take a look at the Geolocation tutorial for more information on how to do this.