闪光-> 字节数组-> AMFPHP-> 图片无效?

发布于 2024-07-24 05:44:39 字数 1975 浏览 11 评论 0原文

我将图像加载到 Flash 中,并使用 JPGEncoder 将图像编码为 ByteArray,并将其发送到 AMF PHP,AMF PHP 将字节数组写入文件。 这一切似乎都工作正常,我可以在 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);

 }

非常感谢你帮忙

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

真心难拥有 2024-07-31 05:44:39

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

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

梦晓ヶ微光ヅ倾城 2024-07-31 05:44:39

您的问题出在您的 PHP 服务上。 在 AMFPHP 中,POST 数据是抽象的,因此 AMFPHP UploadService 脚本中需要一个函数,该函数接受 _uploadService.call 中的两个输入参数 --jpg_binary 和 currentImageFilename-- ,如下所示:

<?php
class UploadService {

     function receiveByteArray( $ba, $filename ) {

          $result = file_put_contents($filename, $ba->data);
          if ( $result == FALSE ) {
              trigger_error( "File save failed" );
          }
     }
}
?>

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" );
          }
     }
}
?>
倾城月光淡如水﹏ 2024-07-31 05:44:39
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 );

对我来说它工作得很好!

奥利弗

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文