CodeIgniter Download the image, and then resize it.

I am trying to upload images and resize them while the work is loading, as well as creating thumbnail images. But I can not change the original image. All that happens is a file that is directly uploaded to the folder in its original size. I assume that I am trying to resize this image and then overwrite the originally downloaded file.

Here is my code that I am missing:

//Initialise the upload file class $config['upload_path'] = './image_uploads/'; $config['allowed_types'] = 'jpg|jpeg|gif|png'; $config['max_size'] = '6048'; $this->load->library('upload', $config); $userfile = "_upload_image"; //check if a file is being uploaded if(strlen($_FILES["_upload_image"]["name"])>0){ if ( !$this->upload->do_upload($userfile))//Check if upload is unsuccessful { $upload_errors = $this->upload->display_errors('', ''); $this->session->set_flashdata('errors', 'Error: '.$upload_errors); redirect('admin/view_food/'.$food->course_id); } else { //$image_data = $this->upload->data(); ////[ THUMB IMAGE ] $config2['image_library'] = 'gd2'; $config2['source_image'] = $this->upload->upload_path.$this->upload->file_name; $config2['new_image'] = './image_uploads/thumbs'; $config2['maintain_ratio'] = TRUE; $config2['create_thumb'] = TRUE; $config2['thumb_marker'] = '_thumb'; $config2['width'] = 75; $config2['height'] = 50; $this->load->library('image_lib',$config2); if ( !$this->image_lib->resize()){ $this->session->set_flashdata('errors', $this->image_lib->display_errors('', '')); } //[ MAIN IMAGE ] $config['image_library'] = 'gd2'; $config['source_image'] = $this->upload->upload_path.$this->upload->file_name; //$config['new_image'] = './image_uploads/'; $config['maintain_ratio'] = TRUE; $config['width'] = 250; $config['height'] = 250; $this->load->library('image_lib',$config); if ( !$this->image_lib->resize()){ $this->session->set_flashdata('errors', $this->image_lib->display_errors('', '')); } } } 
+6
source share
3 answers

You tried to add this to the configuration settings

 $config['overwrite'] = TRUE; 

That should do the trick.

According to the documentation

If set to true, if the file with the same name as you upload, it will be overwritten. If set to false, a number will be added to the file name if another name exists with the same name.

+4
source

I tried to create sketches in the last 20 hours, and I was not successful until I realized where I messed up the code, I was not INITIALIZING my configuration array !!!

Just add this line of code after loading image_lib

 $this->image_lib->initialize($config); 

Hope this works for you ........... thumbs up!

+3
source

If you enjoy playing with Image with CI, I highly recommend the image_moo library.

0
source

Source: https://habr.com/ru/post/912484/


All Articles