PHP receives binary data stream and converts it into pictures_PHP tutorial

WBOY
Release: 2016-07-13 17:50:10
Original
853 people have browsed it

Php code
1 2 /**
3 * Picture category
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 //No data flow
12 const NO_STREAM_DATA = 'The post data is empty';
13 //The image type is incorrect
14 const NOT_CORRECT_TYPE = 'Not a correct image type';
15 //Cannot create file
16 const CAN_NOT_CREATE_FILE = 'Can not create file';
17 //Upload picture name
18 public $image_name;
19 //Picture saving name
20 public $save_name;
21 //Picture saving path
22 public $save_dir;
23 //Directory + full path to image
24 public $save_fullpath;
25
26 /**
27 * Constructor
28 * @param String $save_name Save the picture name
29 * @param String $save_dir save path name
30*/
31 public function __construct($save_name, $save_dir) {
32                //set_error_handler ( $this->error_handler () );
33 
34               //Set the name of the saved picture, if not set, a unique file name will be randomly generated
35          $this->save_name = $save_name ? $save_name : md5 ( mt_rand (), uniqid () );
36                 //Set the path to save the picture, if not set, use the year/month/day format for directory storage
37          $this->save_dir = $save_dir ? self::ROOT_PATH .$save_dir : self::ROOT_PATH .date ( 'Y/m/d' );
38 
39                   //Create folder
40           @$this->create_dir ($this->save_dir);
41                //Set directory + full path of picture www.2cto.com
42          $this->save_fullpath = $this->save_dir . '/' . $this->save_name;
43 }
44 //Compatible with PHP4
45 public function image($save_name) {
46          $this->__construct ($save_name);
47 }
48
49 public function stream2Image() {
50            //Binary data stream
51         $data = file_get_contents ( 'php://input' ) ? file_get_contents ( 'php://input' ) : gzuncompress ( $GLOBALS ['HTTP_RAW_POST_DATA'] );
52              //If the data stream is not empty, save the operation
53 if (! empty ( $data )) {
54                  //Create and write the data stream, and then save the file
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 * Create folder
74 * @param String $dirName Folder path name
75*/
 76     public function create_dir($dirName, $recursive = 1,$mode=0777) {
 77         ! is_dir ( $dirName ) && mkdir ( $dirName,$mode,$recursive );
 78     }
 79     /**
80 * Get image information and return the width, height, type, size, and image mine type of the image
81 * @param String $imageName Image name
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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478298.htmlTechArticlePhp代码 1 ?php 2 /**3 * Picture category 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;...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template