Wednesday, March 31, 2010

Server-side interaction in Flash:

In this tutorial i just want to describe you how to send data from Flash to a server side language. In this tutorial shows the basics of this common practice using PHP as the server side language.


stop();
var _score:Number;
var _levelNo:int;
var myVars: LoadVars = new LoadVars();
myVars.load("http://*.*.*.*/Flash/serverScriptExample/test.php");
myVars.onLoad = function(success) {
if (success) {
_score = myVars.score;
_levelNo = myVars.level;
} else {
trace("File Failed to open!");
}
};

The latest Flash MX has introduced a new Object type called LoadVars() that simplify the exchange of data between client and server.
On the PHP side, things are simple, since the way to return data to Flash is a query basically to return the two variables to the Flash movie you can output something like "score=120&lives=2".

This can be done in PHP with something like this:



$score = 120;
$level = 2;
echo "score=$score&level =$level ";


The techniques shown in this article can be used also to interact with other application servers, based on other languages such as ASP .NET, JSP etc
There's another useful method in the LoadVar object called SendAndLoad() which let you exchange data with the server in a bi-directional way. Here's an example :


// myVars is LoadVars object
var myVars:LoadVars = new LoadVars();
// these are the vars the are going to be sent
myVars.score = _score;
myVars.levelNo = _levelNo;
// handling the load event
loadObj.onLoad = function(success) {
if ((success)) {
trace("loading successfully");
} else {
trace("loading problem");
}
};
// Send and Load data
myVars.sendAndload(("http://*.*.*.*/Flash/serverScriptExample/test.php","myVars","post");



This can be done in PHP with something like this:


$score = $_POST[ ' _score ' ];
$levelNo = $_POST[ ' _levelNo' ];


Basically, you can send variables from flash to any serverside programming language. At the serverside (such as PHP), you get the data using the $_POST["var1"] or $_GET["var2"] depends on you send variables as POST or GET. When you want to send data from PHP back to flash, try to read this method: LoadVars().

Wednesday, March 3, 2010

Save image with flash and php


Here is the code to save the image through flash action script 2.0. Use the BitmapData class in action script 2.0 and get the data in string format that is basically value of each pixel and send the data 2 PHP file on the server. PHP file would prepare the header of the required image format and save the JPEG, PNG on the server as well and then at last when JPEG, PNG is saved than prompt user to download JPEG, PNG on their PC. :)
Action script 2.0
stop();
import flash.display.*;
 function sendImg () {
                var bmpData:BitmapData= new BitmapData(fighter_mc._width, fighter_mc._height);
                bmpData.draw(fighter_mc);
                var _pixels:Array = new Array();
                var _w:Number = bmpData.width;
                var _h:Number = bmpData.height;
                for (var i = 0; i<=_w; i++) {
                                for (var j = 0; j<=_h; j++) {
                                                var tmp = bmpData.getPixel(i,j ).toString(16);
                                                _pixels.push(tmp);
                                }
                }
                var lv:LoadVars = new LoadVars();
                lv.img = _pixels.toString();
                lv.height = _h;
                lv.width = _w;
                lv. sendAndLoad ("show.php", " lv ", "POST");
}
doneBtn.onPress = function() {
                sendImg();
};


PHP:
                $data = explode(",", $_POST['img']);
                $width = $_POST['width'];
                $height = $_POST['height'];
               
                $image=imagecreatetruecolor( $width ,$height );
                $background = imagecolorallocate( $image ,0 , 0 , 0 );
                //Copy pixels
                $i = 0;
                for($x=0; $x<=$width; $x++){
                                for($y=0; $y<=$height; $y++){
                                                $int = hexdec($data[$i++]);
                                                $color = imageColorAllocate ($image, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int);
                                                imagesetpixel ( $image , $x , $y , $color );
                                }
                }
                //Output image and clean
                header( "Content-type: image/jpeg" );
                ImageJPEG( $image );
                imagedestroy( $image );              
?>