為什麼 JPGEncoded bytearray 從 AS3 發送到 AMFPHP 會導致圖像無效? (Why is JPGEncoded bytearray sent from AS3 to AMFPHP resulting in an invalid image?)


問題描述

為什麼 JPGEncoded bytearray 從 AS3 發送到 AMFPHP 會導致圖像無效? (Why is JPGEncoded bytearray sent from AS3 to AMFPHP resulting in an invalid image?)

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? 

Thanks - here is some 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>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);

 }

參考解法

方法 1:

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" );
          }
     }
}
?>

(by undefinedbob quinn)

參考文件

  1. Why is JPGEncoded bytearray sent from AS3 to AMFPHP resulting in an invalid image? (CC BY-SA 3.0/4.0)

#bytearray #amfphp #jpeg #actionscript-3 #bitmapdata






相關問題

更改創建 XML 閱讀器時使用的 XmlDictionaryReaderQuotas 對象的 MaxArrayLength 屬性 (Changing the MaxArrayLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader)

將文件讀入 4 個字節的 ByteArrays (Read file into ByteArrays of 4 bytes)

使用套接字 (C#) 進行文件傳輸 - 接收到的文件不包含完整數據 (File transfer using sockets (C#) - received file doesn't contain full data)

將 Java 字節讀取器轉換為 InputStream (Converting a Java byte reader to an InputStream)

用 bigInteger 和其他方法重建字節數組 (Rebuild byte array with bigInteger and other method)

如何將字節數組作為參數發送到 HTML.Action? (How to send byte array as paramater to HTML.Action?)

如何將存儲在字節數組中的圖像加載到 WebView? (How to load an image stored in a byte array to WebView?)

我希望將畫布保存為 mySql blob 字段中的 blob (I am looking to save a canvas as a blob in mySql blob field)

將字節數組的部分轉換為整數值 (Converting sections of byte arrays to integer values)

從內存中釋放 jni 引用 (Freeing jni references from memory)

為什麼 JPGEncoded bytearray 從 AS3 發送到 AMFPHP 會導致圖像無效? (Why is JPGEncoded bytearray sent from AS3 to AMFPHP resulting in an invalid image?)

Java:字節到浮點數/整數 (Java: Bytes to floats / ints)







留言討論