php接收二进制数据流转换成图片

来源:互联网 发布:数据库原理课后题答案 编辑:程序博客网 时间:2024/05/16 05:32
  1. <?php  
  2.   
  3. class image  
  4.     const ROOT_PATH './' 
  5.     const FAIL_WRITE_DATA 'Fail to write data' 
  6.     //没有数据流  
  7.     const NO_STREAM_DATA 'The post data is empty' 
  8.     //图片类型不正确  
  9.     const NOT_CORRECT_TYPE 'Not correct image type' 
  10.     //不能创建文件  
  11.     const CAN_NOT_CREATE_FILE 'Can not create file' 
  12.     //上传图片名称  
  13.     public $image_name 
  14.     //图片保存名称  
  15.     public $save_name 
  16.     //图片保存路径  
  17.     public $save_dir 
  18.     //目录+图片完整路径  
  19.     public $save_fullpath 
  20.       
  21.       
  22.     public function __construct($save_name$save_dir 
  23.         //set_error_handler $this->error_handler () );  
  24.           
  25.         //设置保存图片名称,若未设置,则随机产生一个唯一文件名  
  26.         $this->save_name $save_name $save_name md5 mt_rand (), uniqid () );  
  27.         //设置保存图片路径,若未设置,则使用年/月/日格式进行目录存储  
  28.         $this->save_dir  $save_dir self::ROOT_PATH .$save_dir self::ROOT_PATH .date 'Y/m/d' );  
  29.            
  30.         //创建文件夹  
  31.         @$this->create_dir $this->save_dir );  
  32.         //设置目录+图片完整路径  
  33.         $this->save_fullpath $this->save_dir '/' $this->save_name;  
  34.      
  35.     //兼容PHP4  
  36.     public function image($save_name 
  37.         $this->__construct $save_name );  
  38.      
  39.       
  40.     public function stream2Image()  
  41.         //二进制数据流  
  42.         $data file_get_contents 'php://input' file_get_contents 'php://input' gzuncompress $GLOBALS ['HTTP_RAW_POST_DATA');  
  43.         //数据流不为空,则进行保存操作  
  44.         if (! emptyempty $data ))  
  45.             //创建并写入数据流,然后保存文件  
  46.             if (@$fp fopen $this->save_fullpath, 'w+' ))  
  47.                 fwrite $fp$data );  
  48.                 fclose $fp );  
  49.                 $baseurl "http://" $_SERVER ["SERVER_NAME"":" $_SERVER ["SERVER_PORT"dirname $_SERVER ["SCRIPT_NAME"'/' $this->save_name;                  
  50.                 if $this->getimageInfo $baseurl ))  
  51.                     echo $baseurl 
  52.                 else  
  53.                     echo self::NOT_CORRECT_TYPE  );  
  54.                  
  55.             else  
  56.               
  57.              
  58.         else  
  59.             //没有接收到数据流  
  60.             echo self::NO_STREAM_DATA );  
  61.          
  62.      
  63.       
  64.     public function create_dir($dirName$recursive 1,$mode=0777)  
  65.         is_dir $dirName && mkdir $dirName,$mode,$recursive );  
  66.      
  67.       
  68.     public function getimageInfo($imageName '' 
  69.         $imageInfo getimagesize $imageName );  
  70.         if ($imageInfo !== false)  
  71.             $imageType strtolower substr image_type_to_extension $imageInfo [2] ), );  
  72.             $imageSize filesize $imageInfo );  
  73.             return $info array ('width' => $imageInfo [0], 'height' => $imageInfo [1], 'type' => $imageType'size' => $imageSize'mine' => $imageInfo ['mine');  
  74.         else  
  75.             //不是合法的图片  
  76.             return false;  
  77.          
  78.       
  79.      
  80.       
  81.       
  82.   

0 0
原创粉丝点击