public function change_avatar()
{
$user_id = $this->input->get('user_id');
//原图配置
$file_dir = ZLMM_SOURCE_PATH.'files/head/300/'.date("Ymd/").rand(0,40);
$file_name= time().rand(1,10000);
$config['upload_path'] = $file_dir;
$config['allowed_types']= '*';
$config['file_name'] = $file_name;
//其他缩略图的配置
$file_dir2= str_replace('/300/','/50/',$file_dir);
$file_dir3= str_replace('/300/','/100/',$file_dir);
$file_dir4= str_replace('/300/','/150/',$file_dir);
if(!file_exists($file_dir)){
mkdir($file_dir,0777,true);//原图路径
mkdir($file_dir2,0777,true);//50*50路径
mkdir($file_dir3,0777,true);//100*100路径
mkdir($file_dir4,0777,true);//150*150路径
}
//50*50图片
$config2 = array(
'image_library' => 'gd2',
'source_image' => '',
'create_thumb' => TRUE,
'thumb_marker' => '',
'maintain_ratio'=> TRUE,
'new_image' => $file_dir2,
'width' => 50,
'height' => 50
);
//100*100图片
$config3 = array(
'image_library' => 'gd2',
'source_image' => '',
'create_thumb' => TRUE,
'thumb_marker' => '',
'maintain_ratio'=> TRUE,
'new_image' => $file_dir3,
'width' => 100,
'height' => 100
);
//300*300图片
$config4 = array(
'image_library' => 'gd2',
'source_image' => '',
'create_thumb' => TRUE,
'thumb_marker' => '',
'maintain_ratio'=> TRUE,
'new_image' => $file_dir4,
'width' => 150,
'height' => 150
);
$this->load->library('upload',$config);
$this->load->library('image_lib');
$st = $this->upload->do_upload('file');
if (!$st){
$arr = array(
'code' => 0,
'msg' => $this->upload->display_errors(),
'data' => array()
);
}else{
$data= $this->upload->data();
//初始化50*50
$config2['source_image'] = $data['full_path']; //文件路径带文件名
$this->image_lib->initialize($config2);
$this->image_lib->resize();
//初始化100*100
$config3['source_image'] = $data['full_path']; //文件路径带文件名
$this->image_lib->initialize($config3);
$this->image_lib->resize();
//初始化300*300
$config4['source_image'] = $data['full_path']; //文件路径带文件名
$this->image_lib->initialize($config4);
$this->image_lib->resize();
$path= $file_dir.'/'.$data['file_name'];
$src = str_replace(ZLMM_SOURCE_PATH,ZLMM_SOURCE_PATH_URL,$path); //返回页面的src
$save_src = str_replace(ZLMM_SOURCE_PATH,'',$path); //保存到数据库的src
$user_arr = array(
'id' => $user_id,
'avatar' => $save_src
);
$st = $this->user_model->update_field($user_arr); //user保存更新数据
if($st['ret']==1){
$pic_arr = array(
'src' => $src,
'id' => $file_name
);
$arr = array(
'code' => 1,
'msg' => '头像修改成功',
'data' => $pic_arr
);
}else{
$arr = array(
'code' => 0,
'msg' => '头像保存成功,数据更新失败。',
'data' => array()
);
}
}
echo json_encode($arr);
}