Php code 1 & lt ;? Php2/** 3 * Image class 4 * @ authorHaroldphp@163.com5 * @ version1.06 * 7 */8 classimage {9constROOT_PATH & amp; #39;./& amp; #39; 10constFAIL_WR
Php code
1 2 /**
3 * Image
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 // the file cannot be created
16 const CAN_NOT_CREATE_FILE = 'can not create file ';
17 // upload the image name
18 public $ image_name;
19 // save the image name
20 public $ save_name;
21 // image storage path
22 public $ save_dir;
23 // Directory + complete Image path
24 public $ save_fullpath;
25
26 /**
27 * constructor
28 * @ param String $ save_name save the image 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 Image. if not set, a unique file name is randomly generated.
35 $ this-> save_name = $ save_name? $ Save_name: md5 (mt_rand (), uniqid ());
36 // set the path for saving the image. if not set, the directory is stored in the year/month/day format.
37 $ this-> save_dir = $ save_dir? Self: ROOT_PATH. $ save_dir: self: ROOT_PATH. date ('Y/m/D ');
38
39 // create a folder
40 @ $ this-> create_dir ($ this-> save_dir );
41 // Set the Directory + complete Image path 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 streams
51 $ data = file_get_contents ('php: // input ')? File_get_contents ('php: // input'): gzuncompress ($ GLOBALS ['http _ RAW_POST_DATA ']);
52 // save the data stream if it is not empty
53 if (! Empty ($ data )){
54 // create and write data streams, and 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 // no data stream received
69 echo (self: NO_STREAM_DATA );
70}
71}
72 /**
73 * create a folder
74 * @ param String $ dirName folder path
75 */
76 public function create_dir ($ dirName, $ recursive = 1, $ mode = 0777 ){
77! Is_dir ($ dirName) & mkdir ($ dirName, $ mode, $ recursive );
78}
79 /**
80 * obtain the image information and return the image width, height, type, size, and image mine type.
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 // invalid image
91 return false;
92}
93
94}
95
96/* private function error_handler ($ a, $ B ){
97 echo $ a, $ B;
98 }*/
99
100}
From myphoebe