phpcodeigniterimage-resizing

CodeIgniter image resize: where to insert call to image_lib>


this is doing my head in - this function is called by an image upload form, and I simply want to resize the image after it's been uploaded - I just can't seem to get it working with the Image_lib library...the function itself works great.

function saveUploadPin()
 {
     $insert['description']     = $this->input->post('description');
     $insert['user_id']         =  $user_id = $this->session->userdata('login_user_id');
     $insert['board_id']        = $boardId = $this->input->post('board_id');
     $insert['type']            = $this->input->post('type');
     $insert['source_url']      = $this->input->post('link');
     $insert['category']      = $this->input->post('category'); 


     if($_FILES["pin"]["name"]!='')
     {
        if ((($_FILES["pin"]["type"] == "image/gif")|| ($_FILES["pin"]["type"] == "image/jpeg")|| ($_FILES["pin"]["type"] == "image/pjpeg") || ($_FILES["pin"]["type"] == "image/png")|| ($_FILES["pin"]["type"] == "image/PNG")|| ($_FILES["pin"]["type"] == "image/GIF")|| ($_FILES["pin"]["type"] == "image/JPG")|| ($_FILES["pin"]["type"] == "image/JPEG")))
        {
            if ($_FILES["pin"]["error"] > 0)
            {
                echo "Return Code: " . $_FILES["pin"]["error"] . "<br />";
            }
            else
            {
                $image          = $_FILES["pin"]["name"];
                $ext            = explode('/', $_FILES["pin"]["type"]);
                $image          = time().'_'.$image;
                $image = str_replace(' ', '_', $image); 
                $dir = getcwd()."/application/assets/pins/$user_id";


                if(file_exists($dir) && is_dir($dir))
                {

                }
                else{

                  mkdir(getcwd()."/application/assets/pins/$user_id",0777);
                }
                move_uploaded_file($_FILES["pin"]["tmp_name"],
                getcwd()."/application/assets/pins/$user_id/" . $image);
                $image = site_url("/application/assets/pins/$user_id/".$image);

            }
            $insert['pin_url']      = $image;
            //$insert['source_url']      = '';
            $id= $this->board_model->saveUploadPin($insert);
            if($id)
            {
               redirect('board/pins/'.$boardId.'/'.$id);
            }

          }
          else
          {
             redirect('board/index/'.$boardId);
          }
    }
 }

Should the call be made after move_uploaded_file with something like this?

$config['image_library'] = 'gd2';
$config['source_image'] = 'not sure what to put here?';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width']     = 550;
$this->image_lib->initialize($config);
$this->image_lib->resize();

I am loading the Image_lib with auto loader. I'm wondering if theres an issue with the path to source_image?

Any help and encouragement would be appreciated, thanks.


Solution

  • Use ci's upload class for file uploading and handling the return values, trust me its much easier.
    And yes, U got it right.

    $destination = getcwd()."/application/assets/pins/$user_id/" . $image;
    move_uploaded_file($_FILES["pin"]["tmp_name"],$destination);
    
    $config['source_image'] = $destination;
    


    Update:

    $image = site_url("/application/assets/pins/$user_id/".$image);
    //This will return http://base_url/index.php/application/assets/pins/$user_id/$image,
    //if in your config file $config['index_page'] = 'index.php';
    //But base_url("/application/assets/pins/$user_id/".$image); will return without index.php
    //Assuming that your $config['base_url'] = 'http://base_url';