Flash 上传 ByteArray 数据到 php 并保存为图片

Flash 可以通过各种途径获取或生成图片的 ByteArray 数据, 尤其是 Flash Player 10 增加了 FileReference.load 方法之后, 更是方便了许多, 最典型的一个应用场景就是用 Flash 编辑图片.

在 player 10 以前, 通常的做法是: 打开图片 -> 上传 -> 返回图片地址 -> 加载 -> 处理 -> 再上传.

player 10 以后就方便了, 直接用 load 方法打开本地图片, 用 Loader.loadBytes 方法显示图片就完成了上面说的前四步.

最终上传也很简单, AS 代码如下:

 

[java]  view plain copy print ?
  1. var uper:URLLoader = new URLLoader();  
  2. var ur:URLRequest = new URLRequest(UP_URL);  
  3. ur.contentType = 'application/octet-stream';  
  4. ur.method = URLRequestMethod.POST;  
  5. ur.data = PNGEncoder.encode(img); // 见参考中的 as3corelib  
  6. uper.load(ur);  


php 接收数据保存图片代码:

 

 

  1. $uuid = uniqid();  
  2.   
  3. $path = sprintf('upload/%s/%s/%s/'date('Y'), date('m'), date('d'));  
  4. $file = sprintf('%s%s.png'$path$uuid);  
  5.   
  6. if(!file_exists($path))  
  7. {  
  8.     mkdir($path, 0755, true);  
  9. }  
  10.   
  11. $img = file_get_contents('php://input');  
  12.   
  13. $fp = fopen($file'w');  
  14.   
  15. fwrite($fp$img);  
  16. fclose($fp);  
  17.   
  18. echo $file;  


相关参考:

猜你喜欢

转载自qianxunniao.iteye.com/blog/1462230