拇指创建的问题

时间:2011-03-11 14:47:47

标签: codeigniter

我目前正在使用codeigniter 2.0进行项目,我在创建拇指时遇到了一些问题。我打算允许用户上传图片并将图片重命名为他们的姓氏,然后在上传功能中创建一个缩略图,然后将其发送到图像目录内的另一个目录(我将其命名为avatar)。代码完美地完成了这一点,但现在我打算在更改上传的pic名称之前加密姓氏。这就是问题出现的地方。上传是在名称改变的情况下进行的,但是没有创建缩略图...请任何人告诉我这里的问题是代码示例

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_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 

1 个答案:

答案 0 :(得分:0)

您的源图片是一个目录。它应该是文件路径。