用pngencoder php(AMFPHP) 保存图片

转自:http://hi.baidu.com/harry1299/blog/item/d7a64e323ec976f01b4cff3a.html

1.Flash端,通过adobe的类包,将BitmapData数据Encoder成ByteArray .然后,通过http或者amfphp发送到服务器端。
Code:

var bd:BitmapData = new   BitmapData(canvas.width,canvas.height);
bd.draw(canvas); 
var jpgEncoder : PNGEncoder = new PNGEncoder(); 
var jpgBytes:ByteArray = PNGEncoder.encode(bd);

2.服务端的工作(针对amfphp而言):接受ByteArray ,注意,php是直接支持ByteArray类型的,然后通过fwrite或者file_put_contents,写数据,并且输出文件,ok搞定
Code:

public function createSavePngFile($ba,$savePoint)
{
       date_default_timezone_set('Asia/Shanghai');
 
       $now = date("YmdHis"); 
       $path = "resource/shareVideo/"; 
       $fileName = $path.$now.".png";
       $data = $ba->data;
 
       if(!file_exists($path.$now.".png"))
       {         
             $input = file_put_contents( $fileName, $data);//写入二进制数据
 
              if($input != 0 && $input != -1)
             {
                   return $this->serverPoxy->updateShareInfo($savePoint,$fileName);
            }
             else
            {
                   return false;
            }
       }
      return false;
}

注意:file_put_contents( $fileName, $data);中第二个参数是:$ba->data,而不是$ba,这是php的数据结构的规范。我就是搞错了这个,浪费了大侠我整个一个上午的时间,可恶^_^.
ok.如果还有什么问题,请联系我,E-Mail:harry_1299@163.com
附上关于AMFPHP网站的参考资料:
Send and Receive ByteArray to AMFPHP(http://www.sephiroth.it/tutorials/flashPHP/amfphp_bytearray/)
Code:

<?php
 
class SaveJPEG
{
     var $output_dir = "temp";
     var $server_url = "http://www.sephiroth.it/amfphp2/services/tutorials/amfphp_bytearray/";
 
    /**
      * Save image from the given bytearray
      * and return the path of the saved image
      */
    function SaveAsJPEG($ba, $compressed = false)
     {
         if(!file_exists($this->output_dir) || !is_writeable($this->output_dir))
            trigger_error ("please create a 'temp' directory first with write access", E_USER_ERROR);
 
        $data = $ba->data;
         if($compressed)
         {
             if(function_exists(gzuncompress))
             {
                $data = gzuncompress($data);
             } else {
                trigger_error ("gzuncompress method does not exists, please send uncompressed data", E_USER_ERROR);
             }
         }
        file_put_contents($this->output_dir . "/rawdata.jpeg", $data);
         return $this->server_url . $this->output_dir . "/rawdata.jpeg";
     }
 
    /**
      * Save file from a given bytearray
      * and return a ByteArray from the saved file
      */
    function SaveAsByteArray($ba, $compresses = false)
     {
         if(!file_exists($this->output_dir) || !is_writeable($this->output_dir))
            trigger_error ("please create a 'temp' directory first with write access", E_USER_ERROR);
 
        $data = $ba->data;
         if($compressed)
         {
             if(function_exists(gzuncompress))
             {
                $data = gzuncompress($data);
             } else {
                trigger_error ("gzuncompress method does not exists, please send uncompressed data", E_USER_ERROR);
             }
         }
        file_put_contents($this->output_dir . "/rawdata.rgb", $data);
         return new ByteArray(file_get_contents($this->output_dir . "/rawdata.rgb"));
     }
}
 
?>

发表评论

电子邮件地址不会被公开。 必填项已用*标注