1
votes

I've been attempting to resize images but with no luck! I have the resize inside the upload function but not sure what is happening wrong! The code below shows my upload function (and resize within it):

function do_upload()
    {
        $config['upload_path']   = './uploads/';        
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '2048';




        $this->load->library('image_lib', $config); 


        $this->load->library('upload', $config);
        $fullImagePath = '';
              if (! $this->upload->do_upload())
              {
                            $this->upload->display_errors('<p style="color: maroon; font-size:large;">', '</p>');

                   $error = array('file_error' => $this->upload->display_errors());
                  // print_r($error);

                   $this->load->view('layout/header');
                   $this->load->view('add_gallery', $error);
                   $this->load->view('layout/footer');

              }else{
                    //echo "UPLOAD SUCCESS";
                  // set a $_POST value for 'image' that we can use later
                        /*Image Manipulation Class*/

                    $config['image_library'] = 'gd2';
                    $config['source_image'] = $fullImagePath;
                    echo $fullImagePath;
                    $config['create_thumb'] = FALSE;
                    $config['maintain_ratio'] = TRUE;
                    $config['max_width'] = '480';
                    $config['max_height'] = '640';
                    $this->load->library('image_lib', $config); 
                    $this->image_lib->resize();


                  $upload_data    = $this->upload->data();
                  $fullImagePath = '/uploads/' . $upload_data['file_name']; 
              }



        return $fullImagePath;
    }

The upload works fine and I get the fullImagePath (link) to store in database. ANyway, just not sure how to handle the resize.

1
your codes looks correct.. make sute $fullImagePath is correct and is pointing to your imahe file.. - bipen

1 Answers

0
votes

The resize() function requires that you first have configured the source_image before calling it, so it can apply the resize to the specified image.

You are sending an empty path. Here you declare the path as empty string $fullImagePath = ''; and then here you define it to the configuration options $config['source_image'] = $fullImagePath; and after that you call the $this->image_lib->resize(); , so it can not do resize to an empty image.

Also you are loading twice the image_lib which is not needed.

I modified your code. Test it and see if it works

function do_upload()
{
    $config['upload_path']   = './uploads/';        
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['max_size'] = '2048';
    $this->load->library('upload', $config);
    $fullImagePath = '';
  if (! $this->upload->do_upload()){
        $this->upload->display_errors('<p style="color: maroon; font-size:large;">', '</p>');
        $error = array('file_error' => $this->upload->display_errors());
        $this->load->view('layout/header');
        $this->load->view('add_gallery', $error);
        $this->load->view('layout/footer');
  }else{
        $upload_data = $this->upload->data();
        $fullImagePath = '/uploads/' . $upload_data['file_name']; 
        $config['image_library'] = 'gd2';
        $config['source_image'] = $fullImagePath;
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = TRUE;
        $config['max_width'] = '480';
        $config['max_height'] = '640';
        $config['width'] = 75;
        $config['height'] = 50;
        $this->load->library('image_lib', $config); 
        $this->image_lib->resize();
  }

return $fullImagePath;
}