Flash AS3 send binary data to PHP and save it as a picture via PHP.
As end:
1 Package2 {3 Importcom. Jpegencoder.jpgencoder;4 ImportFlash.display.Bitmap;5 ImportFlash.display.BitmapData;6 ImportFlash.display.DisplayObject;7 ImportFlash.display.Loader;8 ImportFlash.display.Sprite;9 Importflash.events.Event;Ten ImportFlash.net.URLLoader; One ImportFlash.net.URLLoaderDataFormat; A Importflash.net.URLRequest; - ImportFlash.net.URLRequestMethod; - ImportFlash.utils.ByteArray; the - /** - * ... - * @author Frost.yen + */ - Public classFlashsendextendsSprite + { A Private varLdr:loader =NewLoader; at Private varBmp:bitmap; - Private varScale Number= 1;//Object Zoom ratio - Public functionflashsend () - { -Ldr.load (NewURLRequest ("11.jpg")); - //image address; in Ldr.contentLoaderInfo.addEventListener (event.complete,onimageloaded); - } to + Private functionOnimageloaded (e:event):void - { theBMP =e.target.content as Bitmap; * /** The scaling and visible of objects do not affect the sending of binary data and the generation of pictures, but if the object has a matte and the registration point is not in the upper-left corner, the image generation will be affected **/ $ //Bmp.scalex = Bmp.scaley = scale;Panax Notoginseng //bmp.visible = false; - AddChild (BMP); the +Sendtophp (Creatbitmapdata (bmp,bmp.width * scale,bmp.height *scale )); A } the //send data to PHP; + }; - $ Private functionCreatbitmapdata (obj:displayobject,_width: Number, _height: Number): BitmapData $ { - varBitmapdata:bitmapdata =NewBitmapData (_width,_height,false, 0XFFFFFF); - Bitmapdata.draw (obj); the returnBitmapData; - }Wuyi Private functionSendtophp (Bitmapdata:bitmapdata):void the { - Wu varJpgencoder:jpgencoder =NewJpgencoder (80); - varJpgstream:bytearray =Jpgencoder.encode (bitmapdata); About varJpgurlrequest:urlrequest =NewURLRequest ("http://localhost:8181/FlashSendToPHPToSaveJPG/toSaveJPG.php");//PHP Address $Jpgurlrequest.data =Jpgstream; -Jpgurlrequest.method =Urlrequestmethod.post; -Jpgurlrequest.contenttype ="Application/octet-stream"; - varJpg_loader:urlloader =NewURLLoader; AJpg_loader.dataformat =urlloaderdataformat.binary; + jpg_loader.load (jpgurlrequest); the } - $ } the the}
PHP Side:
1<?PHP2 //$filename = $_get[jpgname]. ". JPG ";//The name of the image to be generated can be added after the PHP address in the as code. Jpgname=image3 $filename= "Image.jpg";//the name of the picture to generate4 $xmlstr=$GLOBALS[Http_raw_post_data];5 Echo $xmlstr;6 if(Empty($xmlstr))$xmlstr=file_get_contents(' Php://input '); 7 $jpg=$xmlstr;//get the binary raw data from the post.8 $file=fopen($filename, "W");//Open file ready to write9 fwrite($file,$jpg);//WriteTen fclose($file);//Close One?>
Here are two downloads for the jpgencoder.as and Bitstring.as class addresses:
http://download.csdn.net/source/3205224
http://code.google.com/p/as3corelib/used: Com.adobe.images.JPEGEncoder
[ActionScript 3.0] As using ByteArray to send binary data to PHP to generate pictures