0

I'm now working in Codeigniter, have some issue in resizing the image while uploading. I have tried some code, but not working fine. Below I have added my file upload function:

public function add_to_event()
{
    if (!empty($_FILES['event_image']['name'])) {
        $config['upload_path']   = './images/';
        $config['allowed_types'] = 'jpg|jpeg|png|gif';
        $config['file_name']     = $_FILES['image']['name']; //image upload-url to db and image to system directory

        //Load upload library and initialize configuration
        $this->load->library('upload', $config);
        $this->upload->initialize($config);

        if ($this->upload->do_upload('event_image')) {
            $uploadData = $this->upload->data();
            $image      = $uploadData['file_name'];
        } else {
            $image = '';
        }
    } 
    else {
    $image = '';
    }
    $image ="http://localhost/infi-admin/images/$image";

    $event_title=$this->input->post('event_title');
    $event_description=$this->input->post('event_description');

    $this->load->model('User_model');
    $data= array('event_title' =>$event_title ,
        'event_description' =>$event_description,
        'event_image'=>$image
    );

    $this->User_model->add_to_event_model($data);
    redirect('Site/event_view');
}

this is my controller function to upload image along with other fields

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

This may help get you started:

$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width']         = 75;
$config['height']       = 50;

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

$this->image_lib->resize();


if (!empty($_FILES['event_image']['name'])) {
        $config['upload_path']   = './images/';
        $config['allowed_types'] = 'jpg|jpeg|png|gif';
        $config['file_name']     = $_FILES['image']['name'];//image upload-url to db and image to system directory

        //Load upload library and initialize configuration
        $this->load->library('upload', $config);
        $this->upload->initialize($config);

        if ($this->upload->do_upload('event_image')) //name in db
        {
            $uploadData = $this->upload->data();
            $image      = $uploadData['file_name'];

            $config['image_library'] = 'gd2';
            $config['source_image'] = $config['upload_path'].$config['file_name']
            $config['create_thumb'] = TRUE;
            $config['maintain_ratio'] = TRUE;
            $config['width']         = 75;
            $config['height']       = 50;

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

            $this->image_lib->resize();

            // image name will be $config['upload_path'].$config['file_name'].'_thumb.jpg'
        } 
        else {
            $image = '';
             }
    } 
    else {
        $image = '';
         }
            $image ="http://localhost/infi-admin/images/$image";

 $event_title=$this->input->post('event_title');
    $event_description=$this->input->post('event_description');

                 $this->load->model('User_model');
                 $data= array('event_title' =>$event_title ,
    'event_description' =>$event_description,
    'event_image'=>$image);

 $this->User_model->add_to_event_model($data);
  redirect('Site/event_view');


}

This should definitely be abstracted into a model or service. The controller doesnt need to know how image uploading and resizing is done. Also consider renaming the image file and adding some randomness to the name before saving it.

MDS
  • 497
  • 1
  • 5
  • 16