title: Why is JPGEncoded bytearray sent from AS3 to AMFPHP resulting in an invalid image?
body: <p>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"</p>
  
  <p>Here is the code i use to write the bytearray to a file -</p>
  
  <p>$jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
  file_put_contents($filename, $jpg);</p>
  
  <p>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? </p>
  
  <p>Thanks - here is some code:</p>
  
  <pre><code>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&gt;MAX_WIDTH||bitmap.height&gt;MAX_HEIGHT) {
    resizeBitmap(bitmap);
  }
      uploadResizedImage(bitmap);
  }
  function resizeBitmap(target:Bitmap):void {
      if (target.height&gt;target.width) {
          target.width=MAX_WIDTH;
          target.scaleY=target.scaleX;
      } else if (target.width &gt;= 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);
  
   }
  </code></pre>