Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-13 10:48:33
Original
855 people have browsed it

  Php代码
  1   2 /**
  3  * 图片类
  4  * @author Haroldphp@163.com
  5  * @version 1.0
  6  *
  7  */
  8 class image {
  9     const ROOT_PATH = './';
 10     const FAIL_WRITE_DATA = 'Fail to write data';
 11     //没有数据流
 12     const NO_STREAM_DATA = 'The post data is empty';
 13     //图片类型不正确
 14     const NOT_CORRECT_TYPE = 'Not a correct image type';
 15     //不能创建文件
 16     const CAN_NOT_CREATE_FILE = 'Can not create file';
 17     //上传图片名称
 18     public $image_name;
 19     //图片保存名称
 20     public $save_name;
 21     //图片保存路径
 22     public $save_dir;
 23     //目录+图片完整路径
 24     public $save_fullpath;
 25    
 26     /**
 27      * 构造函数
 28      * @param String $save_name 保存图片名称
 29      * @param String $save_dir 保存路径名称
 30      */
 31     public function __construct($save_name, $save_dir) {
 32         //set_error_handler ( $this->error_handler () );
 33        
 34         //设置保存图片名称,若未设置,则随机产生一个唯一文件名
 35         $this->save_name = $save_name ? $save_name : md5 ( mt_rand (), uniqid () );
 36         //设置保存图片路径,若未设置,则使用年/月/日格式进行目录存储
 37         $this->save_dir =  $save_dir ? self::ROOT_PATH .$save_dir : self::ROOT_PATH .date ( 'Y/m/d' );
 38         
 39         //创建文件夹
 40         @$this->create_dir ( $this->save_dir );
 41         //设置目录+图片完整路径 www.2cto.com
 42         $this->save_fullpath = $this->save_dir . '/' . $this->save_name;
 43     }
 44     //兼容PHP4
 45     public function image($save_name) {
 46         $this->__construct ( $save_name );
 47     }
 48    
 49     public function stream2Image() {
 50         //二进制数据流
 51         $data = file_get_contents ( 'php://input' ) ? file_get_contents ( 'php://input' ) : gzuncompress ( $GLOBALS ['HTTP_RAW_POST_DATA'] );
 52         //数据流不为空,则进行保存操作
 53         if (! empty ( $data )) {
 54             //创建并写入数据流,然后保存文件
 55             if (@$fp = fopen ( $this->save_fullpath, 'w+' )) {
 56                 fwrite ( $fp, $data );
 57                 fclose ( $fp );
 58                 $baseurl = "http://" . $_SERVER ["SERVER_NAME"] . ":" . $_SERVER ["SERVER_PORT"] . dirname ( $_SERVER ["SCRIPT_NAME"] ) . '/' . $this->save_name;               
 59                 if ( $this->getimageInfo ( $baseurl )) {
 60                     echo $baseurl;
 61                 } else {
 62                     echo ( self::NOT_CORRECT_TYPE  );
 63                 }
 64             } else {
 65            
 66             }
 67         } else {
 68             //没有接收到数据流
 69             echo ( self::NO_STREAM_DATA );
 70         }
 71     }
 72     /**
 73      * 创建文件夹
 74      * @param String $dirName 文件夹路径名
 75      */
 76     public function create_dir($dirName, $recursive = 1,$mode=0777) {
 77         ! is_dir ( $dirName ) && mkdir ( $dirName,$mode,$recursive );
 78     }
 79     /**
 80      * 获取图片信息,返回图片的宽、高、类型、大小、图片mine类型
 81      * @param String $imageName 图片名称
 82      */
 83     public function getimageInfo($imageName = '') {
 84         $imageInfo = getimagesize ( $imageName );
 85         if ($imageInfo !== false) {
 86             $imageType = strtolower ( substr ( image_type_to_extension ( $imageInfo [2] ), 1 ) );
 87             $imageSize = filesize ( $imageInfo );
 88             return $info = array ('width' => $imageInfo [0], 'height' => $imageInfo [1], 'type' => $imageType, 'size' => $imageSize, 'mine' => $imageInfo ['mine'] );
 89         } else {
 90             //不是合法的图片
 91             return false;
 92         }
 93    
 94     }
 95    
 96     /*private function error_handler($a, $b) {
 97         echo $a, $b;
 98     }*/
 99
100 }


 

摘自  myphoebe
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template