as3 stage to jpg byte-array to server side php problems
hi im looking to get a test working. embeding a swf that sends a jpeg of the stage to a php script that saves to my server. im in deep water here and cant get the following to work...
//----------------------------AS3
import com.adobe.images.JPGEncoder;
import flash.events.MouseEvent;
import flash.display.Sprite;
var dot:Sprite = new Sprite()
dot.graphics.beginFill(1)
dot.graphics.drawCircle(550/2,400/2,40)
addChild(dot)
stage.addEventListen开发者_如何学Pythoner(MouseEvent.MOUSE_DOWN, saveJPG)
function saveJPG():void{
var jpgSource:BitmapData = new BitmapData (stage.stageWidth, stage.stageHeight);
jpgSource.draw(stage);
var jpgEncoder:JPGEncoder = new JPGEncoder(85);
var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);
var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
var jpgURLRequest:URLRequest = new URLRequest("jpg_encoder_download.php?name=sketch.jpg");
jpgURLRequest.requestHeaders.push(header);
jpgURLRequest.method = URLRequestMethod.POST;
jpgURLRequest.data = jpgStream;
navigateToURL(jpgURLRequest, "_blank");
}
//----------------------- php
<?php
if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
// get bytearray
$jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
imagejpeg( $jpg , "yes.jpg", 100 );
}
?>
I like to use AMFPHP to handle situations like this. Check out the remoting section of Josh Strikes main page here. Just nest the ByteArray into another array holding any other data you need to identify it or just send it by itself. This way you can either decode it and save it to a folder or just stuff the ByteArray into a database to be used later. As stated above you must make sure to encode the ByteArray first.
You may need to encode the byteArray to Base64 before POSTing it, and then decode that on the server side with PHP. But you don't really say what exactly is not working...
You are missing the image conversions from BitMapData to BitMap to Image
function createJPG():void{
var finished1:BitmapData = new BitmapData ( stage.stageWidth, stage.stageHeight, true, 0xffffffff );
finished1.draw( stage );
// the key part you are missing
var myImage:Image = new Image();
myImage.load( new Bitmap(finished1) );
myImage.content.width = stage.stageWidth;
myImage.content.height = stage.stageHeight;
var finished:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0xffffffff );
finished.draw(myImage);
myImage = null;
// encode the finished image so we can send the data to the server
var encoder:JPEGEncoder = new JPEGEncoder();
var data:ByteArray = encoder.encode(finished);
var b64:Base64Encoder = new Base64Encoder()
b64.encodeBytes( data )
return b64.toString();
}
Have a look at this link it will explain all you need
精彩评论