I am currently using codeigniter 2.0 for a project and I have a little problem with thumb creation. I intend to allow users to upload picture and have the picture renamed to their last name then have anoda function from within the upload function create a thumbnail then send it to another directory inside the image directory (I named it avatar). The code does this perfectly, but now I intend to encrypt the last name before changing the uploaded pic name. That is where the problem sets in. The upload is carried out the name is changed but a thumbnail is not created ... please can any one tell me what the problem is here is the code sample
function set_avatar()
{
/*check if user is even logged in */
if($this->session->userdata('user_id'))
{
$last_name=$this->session->userdata('last_name');//this value is encrypted
/*parse the useful user values to the data array for use on this page */
$data['user_id']= $user_id;
$data['email']= $email;
$data['last_name']= $last_name;
$data['first_name']= $first_name;
/*assign an empty value to the error key of the data array to avoid error on the view page*/
$data['error'] = "";
/*the directory for uploading the file the number represents a mixture of birth date and month
af means all files u1 upload 1 avts avatar ..jst a path to make the path undetectable*/
$dir = "./path/avatar";
/*preparing the config settings for upload */
$config['file_name'] = $last_开发者_StackOverflow社区name; // the new file name the image should have
$config['upload_path'] = $dir; // the directory the image should go to,already specified
$config['allowed_types'] = 'gif|jpg|png'; // the file types that are allowed
$config['max_size'] = '10000'; //the maximum image size
$config['max_width'] = '300'; //the maximum image width
$config['max_height'] = '230'; // the maximum image height
$this->load->library('upload', $config); // retrieving the upload library with the specified settings
/*using the do upload settings upload the file if it fails stop upload and show errors*/
if ( ! $this->upload->do_upload())
{
$data['error'] = $this->upload->display_errors();
$this->load->view("user/set_avatar",$data);
}
/*else retrieve the upload data from the file uploaded*/
else
{
/*pass all the upload data to the $img_data array*/
$img_data = $this->upload->data();
/*the file(image) extension type png or gif or jpg*/
$img_ext = $img_data['file_ext'];
/*thumb name without extension*/
$thumb = $last_name;
/*thumb name with extension*/
$thumb_name = $thumb.$img_ext;
/*create thumbnail using the thumb nail function*/
$this->create_thumb($thumb_name);
/*record avatar details in the db*/
$avatar_array = array(
"user_id" => $user_id,
"avatar" => $thumb,
"extension" => $img_ext
);
$this->craft_set_user_details->set_avatar($avatar_array);
/*start creat session for img_name and ext*/
$image_session = array(
"img_name"=>$thumb,
"img_ext"=>$img_ext
);
$this->session->set_userdata($image_session);
/*redirect to home page*/
redirect('home','location');
}//end if else $this->upload->do_upload
}//end check if user is logged in ($this->session->userdata('user_id'))
else
{
redirect("index");
}//end if eles user is logged in ($this->session->userdata('user_id'))
}//end function set avatar
/* this is the create_thumb() function */
///start the create thumb function this is used in the set avatar function to create the avatar thumb
function create_thumb($thumb_name)
{
/*check if user is logged in*/
if($this->session->userdata('user_id'))
{
//set the path to were the image is
$dir = "./path/avatar";
//set the path to were the thumb should be sent
$new = "./path/avatar/thumbs";
//set the configuration to be used for image resizing
$config['image_library'] = 'gd2';
$config['source_image'] = $dir;
$config['new_image'] = $new;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 35;
$config['height'] = 50;
//load the image library for image reszing
$this->load->library('image_lib', $config);
//resize image
$this->image_lib->resize();
}//end if eles user is logged in ($this->session->userdata('user_id'))
//else redirect to the home page
else
{
redirect('index','location');
}
}//end function to create
Your source image is a directory. It should be a file path.
精彩评论