php代码
1 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
http://www.bkjia.com/phpjc/478298.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/478298.htmltecharticlephp代码 1 ?php 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;...