views:

1321

answers:

3

Hi,

Im loading images into Flash and using JPGEncoder to encode the image to a ByteArray and send this to AMF PHP which writes out the bytearray to a file. This all appears to work correctly and I can download the resulting file in Photoshop CS4 absolutely fine. When i try to open it from the desktop or open it back in Flash it doesnt work... Picasa my default image browser says "Invalid"

Here is the code i use to write the bytearray to a file -

$jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
file_put_contents($filename, $jpg);

That's it ... I use the NetConnection class to connect and call the service, do I need to say Im sending jpg data? I assumed that JPGEncoder took care of that. How can I validate the bytearray before writing the file? Do I need to set MIME type or something .. excuse the slightly noob questions, a little knowledge can be a dangerous thing.

Thanks

--------------------------------------- PART II ------------------------------------------

Here is some code -

1) load the image into Flash player

item.load();
function _onImageDataLoaded(evt:Event):void {
  var tmpFileRef:FileReference=FileReference(evt.target);
  image_loader=new Loader  ;
  image_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, _onImageLoaded);
  image_loader.loadBytes(tmpFileRef.data);
}
function _onImageLoaded(evt:Event):void {
bitmap=Bitmap(evt.target.content);
bitmap.smoothing=true;
if (bitmap.width>MAX_WIDTH||bitmap.height>MAX_HEIGHT) {
  resizeBitmap(bitmap);
}
    uploadResizedImage(bitmap);
}
function resizeBitmap(target:Bitmap):void {
if (target.height>target.width) {
 target.width=MAX_WIDTH;
 target.scaleY=target.scaleX;
} else if (target.width >= target.height) {
 target.height=MAX_HEIGHT;
 target.scaleX=target.scaleY;
}

}
function uploadResizedImage(target:Bitmap):void {
var _bmd:BitmapData=new BitmapData(target.width,target.height);
_bmd.draw(target, new Matrix(target.scaleX, 0, 0, target.scaleY));
var encoded_jpg:JPGEncoder=new JPGEncoder(90);
var jpg_binary:ByteArray=encoded_jpg.encode(_bmd);
_uploadService=new NetConnection();
_uploadService.objectEncoding=ObjectEncoding.AMF3
_uploadService.connect("http://.../amfphp/gateway.php");
_uploadService.call("UploadService.receiveByteArray",new Responder(success, error), jpg_binary, currentImageFilename);

 }

Many thanks for you help

A: 

can you check the endian type of the bytearray? maybe it is defaulted to big endian

Jeffrey Chee
I'll check thanks, can you explain why that would cause a problem please? cheers
undefined
Yes, endian = bigEndian. Ive tried littleEndian (jpg_binary.endian = Endian.LITTLE_ENDIAN;) but still get problems. It is odd that photoshop can open the image ok but Flash, picassa and windows image viewer etc can't.
undefined
I've recently written some actionscript in my project and encountered a bug (unable to read the binary data properly), until I realized it is the byte order which is wrong (defaulted to big endian).
Jeffrey Chee
Did BigEndian solve the problem? Where you noticing the same as me?
undefined
A: 

Your problem is in your PHP service. In AMFPHP the POST data is abstracted, so what you need in your AMFPHP UploadService script is a function that accepts the two input arguments in your _uploadService.call --jpg_binary and currentImageFilename-- like this:

<?php
class UploadService {

     function receiveByteArray( $ba, $filename ) {

          $result = file_put_contents($filename, $ba->data);
          if ( $result == FALSE ) {
              trigger_error( "File save failed" );
          }
     }
}
?>
bob quinn
A: 
var dataToBeSent:ByteArray =jpgEncoder.encode(theBitmapData);
var url:String = "upload.php";
var urlReq:URLRequest = new URLRequest(url);
urlReq.data = dataToBeSent;
urlReq.method = URLRequestMethod.POST;
urlReq.contentType = "application/octet-stream";
var urlLoader:URLLoader = new URLLoader();
urlLoader.load(urlReq);

Please mind the contentType line

PHP

$fp = fopen( $fname, "wb" );
fwrite( $fp, $GLOBALS[ 'HTTP_RAW_POST_DATA' ] );
fclose( $fp );

For me it works perfectly!

Oliver

Oliver