php接收二進制數據流轉換成圖片


  1. <?php  
  2. /** 
  3.  * 圖片類 
  4.  * @author Haroldphp@163.com 
  5.  * @version 1.0 
  6.  * 

    PHP默認只識別application/x-www.form-urlencoded標准的數據類型。
    因此,對型如text/xml 或者 soap 或者 application/octet-stream 之類的內容無法解析,如果用$_POST數組來接收就會失敗!
    故保留原型,交給$GLOBALS['HTTP_RAW_POST_DATA'] 來接收。

    另外還有一項 php://input 也可以實現此這個功能

    php://input 允許讀取 POST 的原始數據。和 $HTTP_RAW_POST_DATA 比起來,它給內存帶來的壓力較小,並且不需要任何特殊的 php.ini 設置。php://input和 $HTTP_RAW_POST_DATA 不能用於 enctype="multipart/form-data"。

  1.  */  
  2. class image {  
  3.     const ROOT_PATH = './';  
  4.     const FAIL_WRITE_DATA = 'Fail to write data';  
  5.     //沒有數據流  
  6.     const NO_STREAM_DATA = 'The post data is empty';  
  7.     //圖片類型不正確  
  8.     const NOT_CORRECT_TYPE = 'Not a correct image type';  
  9.     //不能創建文件  
  10.     const CAN_NOT_CREATE_FILE = 'Can not create file';  
  11.     //上傳圖片名稱  
  12.     public $image_name;  
  13.     //圖片保存名稱  
  14.     public $save_name;  
  15.     //圖片保存路徑  
  16.     public $save_dir;  
  17.     //目錄+圖片完整路徑  
  18.     public $save_fullpath;  
  19.       
  20.     /** 
  21.      * 構造函數 
  22.      * @param String $save_name 保存圖片名稱 
  23.      * @param String $save_dir 保存路徑名稱 
  24.      */  
  25.     public function __construct($save_name$save_dir) {  
  26.         //set_error_handler ( $this->error_handler () );  
  27.           
  28.         //設置保存圖片名稱,若未設置,則隨機產生一個唯一文件名  
  29.         $this->save_name = $save_name ? $save_name : md5 ( mt_rand (), uniqid () );  
  30.         //設置保存圖片路徑,若未設置,則使用年/月/日格式進行目錄存儲  
  31.         $this->save_dir =  $save_dir ? self::ROOT_PATH .$save_dir : self::ROOT_PATH .date ( 'Y/m/d' );  
  32.            
  33.         //創建文件夾  
  34.         @$this->create_dir ( $this->save_dir );  
  35.         //設置目錄+圖片完整路徑  
  36.         $this->save_fullpath = $this->save_dir . '/' . $this->save_name;  
  37.     }  
  38.     //兼容PHP4  
  39.     public function image($save_name) {  
  40.         $this->__construct ( $save_name );  
  41.     }  
  42.       
  43.     public function stream2Image() {  
  44.         //二進制數據流  
  45.         $data = file_get_contents ( 'php://input' ) ? file_get_contents ( 'php://input' ) : gzuncompress ( $GLOBALS ['HTTP_RAW_POST_DATA'] );  
  46.         //數據流不為空,則進行保存操作  
  47.         if (! emptyempty ( $data )) {  
  48.             //創建並寫入數據流,然后保存文件  
  49.             if (@$fp = fopen ( $this->save_fullpath, 'w+' )) {  
  50.                 fwrite ( $fp$data );  
  51.                 fclose ( $fp );  
  52.                 $baseurl = "http://" . $_SERVER ["SERVER_NAME"] . ":" . $_SERVER ["SERVER_PORT"] . dirname ( $_SERVER ["SCRIPT_NAME"] ) . '/' . $this->save_name;                  
  53.                 if ( $this->getimageInfo ( $baseurl )) {  
  54.                     echo $baseurl;  
  55.                 } else {  
  56.                     echo ( self::NOT_CORRECT_TYPE  );  
  57.                 }  
  58.             } else {  
  59.               
  60.             }  
  61.         } else {  
  62.             //沒有接收到數據流  
  63.             echo ( self::NO_STREAM_DATA );  
  64.         }  
  65.     }  
  66.     /** 
  67.      * 創建文件夾 
  68.      * @param String $dirName 文件夾路徑名 
  69.      */  
  70.     public function create_dir($dirName$recursive = 1,$mode=0777) {  
  71.         ! is_dir ( $dirName ) && mkdir ( $dirName,$mode,$recursive );  
  72.     }  
  73.     /** 
  74.      * 獲取圖片信息,返回圖片的寬、高、類型、大小、圖片mine類型 
  75.      * @param String $imageName 圖片名稱 
  76.      */  
  77.     public function getimageInfo($imageName = '') {  
  78.         $imageInfo = getimagesize ( $imageName );  
  79.         if ($imageInfo !== false) {  
  80.             $imageType = strtolower ( substr ( image_type_to_extension ( $imageInfo [2] ), 1 ) );  
  81.             $imageSize = filesize ( $imageInfo );  
  82.             return $info = array ('width' => $imageInfo [0], 'height' => $imageInfo [1], 'type' => $imageType'size' => $imageSize'mine' => $imageInfo ['mine'] );  
  83.         } else {  
  84.             //不是合法的圖片  
  85.             return false;  
  86.         }  
  87.       
  88.     }  
  89.       
  90.     /*private function error_handler($a, $b) { 
  91.         echo $a, $b; 
  92.     }*/  
  93.   
  94. }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM