|  | 
 
 
 楼主|
发表于 2010-6-4 13:32:54
|
显示全部楼层 
| //图片路径格式化 function _image_format($size,$image){
 list($prefex,$ext) = explode('.',$image);
 return $prefex.'_'.$size.'.'.$ext;
 }
 
 /*
 * 上传图片
 */
 function upload_image() {
 $this->_checkLogin();
 $temp_file = $_FILES['photo']['tmp_name'];
 $size = $_FILES['photo']['size'];
 $image_type = array('jpg','png','gif','JPG','PNG','GIF');
 $save_path = '/data1/codeigniter/userhead/';
 
 $file_info = explode('.',$_FILES['photo']['name']);
 if(!in_array($file_info[1],$image_type)){
 $this->_outputJsonRet('h00001');  //非法的数据类型
 }
 if($size>1024*1024*3){
 $this->_outputJsonRet('h00002');  //文件不能大于1024*1024*3
 }
 $file_name = $this->user_id.'.'.$file_info[1];
 $targetFile =  $save_path . $file_name;
 if(move_uploaded_file($temp_file,$targetFile)) {
 //        $targetFile_50  = $this->_image_format(50,$targetFile);
 $targetFile_60  = $this->_image_format(60,$targetFile);
 $targetFile_110 = $this->_image_format(110,$targetFile);
 if(!$this->do_resize(110,$targetFile)){
 $this->_outputJsonRet('e00003'); //压缩错误110
 }
 $this->do_water(110,$targetFile_110);
 
 if(!$this->do_resize(60,$targetFile)){
 $this->_outputJsonRet('e00002'); //压缩错误60
 }
 $this->do_water(60,$targetFile_60);
 
 if(!$this->do_resize(50,$targetFile)){
 $this->_outputJsonRet('e00001'); //压缩错误50
 }
 $this->do_water(50,$targetFile);
 
 
 chmod($targetFile, 0755);
 chmod($targetFile_60, 0755);
 chmod($targetFile_110, 0755);
 
 $this->_outputJsonRet('s00001', $file_name );
 } else {
 $this->_outputJsonRet('h00003');  //头像上传失败
 }
 
 }
 
 //压缩图片
 function do_resize($size,$source,$quality = '100%'){
 $config['width']                         = $size;
 $config['height']                         = $size;
 $config['quality']                         = $quality;
 $config['image_library']         = 'gd2';
 $config['source_image']         = $source;
 if($size == 50){
 $config['thumb_marker']                = '';
 } else {
 $config['thumb_marker']                = '_'.$size;
 }
 $config['create_thumb']         = TRUE;
 $config['maintain_ratio']         = TRUE;
 
 $this->image_lib->initialize($config);
 $flag = true;
 if (!$this->image_lib->resize()){
 $flag = false;
 }
 unset($config);
 $this->image_lib->clear();
 return $flag;
 }
 //水印处理
 function do_water($size,$source = ''){
 $config['source_image'] = '';
 switch($size){
 case '110':
 $config['source_image'] = '/data1/codeigniter/userhead/blank_110.jpg';
 break;
 case '60':
 $config['source_image'] = '/data1/codeigniter/userhead/blank_60.jpg';
 break;
 case '50':
 $config['source_image'] = '/data1/codeigniter/userhead/blank_50.jpg';
 break;
 }
 $config['wm_type'] = 'overlay';
 $config['wm_opacity'] = 100;
 $config['wm_vrt_alignment'] = 'middle';
 $config['wm_hor_alignment'] = 'center';
 $config['wm_overlay_path'] = $source;
 $config['new_image'] = $source;
 $this->image_lib->initialize($config);
 
 $this->image_lib->watermark();
 unset($config);
 $this->image_lib->clear();
 
 }
 | 
 |