0

我将图像加载到 Flash 中并使用 JPGEncoder 将图像编码为 ByteArray 并将其发送到 AMF PHP,后者将 bytearray 写入文件。这一切似乎都可以正常工作,我可以在 Photoshop CS4 中下载生成的文件绝对没问题。当我尝试从桌面打开它或在 Flash 中打开它时,它不起作用... Picasa 我的默认图片浏览器显示“无效”

这是我用来将字节数组写入文件的代码 -

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

就是这样......我使用NetConnection类连接并调用服务,我需要说我发送jpg数据吗?我认为 JPGEncoder 会处理这个问题。如何在写入文件之前验证字节数组?我是否需要设置 MIME 类型或其他东西.. 请原谅有点菜鸟的问题,一点知识可能是一件危险的事情。

谢谢

--------------------------------------- 第二部分 --------- ---------------------------------

这是一些代码 -

1) 将图像加载到 Flash 播放器中

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);

 }

非常感谢你的帮助

4

3 回答 3

0

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" );
          }
     }
}
?>
于 2009-10-27T14:51:37.850 回答
0

你能检查字节数组的字节序类型吗?也许它默认为大端

于 2009-06-05T15:57:23.020 回答
0
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);

注意contentType

PHP

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

对我来说,它完美无缺!

奥利弗

于 2010-03-25T07:14:25.103 回答