I’m trying to create a thumbnail for the image that will be uploaded to my server. Refer to https://www.codeigniter.com/userguide3/libraries/image_lib.html#processing-an-image . After the creation, I want to save the thumbnail to another folder since both the original image and the thumbnail will be saved in the same location.
So, I try using move_uploaded_file()
function but it unable to move the file. I’m not really sure if my code is correctly done. Check it out.
Belwo is the my code :
if($_FILES['file_name']['size'] != 0){ $config['upload_path'] = FCPATH.MEDIA_LIB_URI.'facts'; $config['allowed_types'] = 'jpg|jpeg|png|gif'; $config['max_size'] = 0; $newFileName = removeExt($_FILES['file_name']['name']).'-'.uniqueId(); // renaming the file name without the ext $new_name = $newFileName.getExt($_FILES['file_name']['name']); // new file name with the ext $config['file_name'] = $new_name; $this->load->library('upload',$config); $this->upload->initialize($config); if($this->upload->do_upload('file_name')){ $uploadData = $this->upload->data(); $file_name = $uploadData['file_name']; $this->load->library('image_lib'); $configer = array( 'image_library' => 'gd2', 'source_image' => $uploadData['full_path'], 'create_thumb' => TRUE, 'maintain_ratio' => TRUE, 'width' => 950, 'height' => 950, ); $this->image_lib->clear(); $this->image_lib->initialize($configer); $this->image_lib->resize(); $thumbName = $newFileName.'_thumb'.getExt($_FILES['file_name']['name']); // getting the exact thumbnail name that been created by codeigniter move_uploaded_file($thumbName, FCPATH.MEDIA_LIB_URI.'facts/thumbnail/'.$new_name); return $file_name; } else { return $this->upload->display_errors(); } }
Advertisement
Answer
Hope this will help you :
Use new_image
in your $configer
to change the folder path : $config['new_image'] = '/path/to/new_image.jpg';
Note : If only the new image name is specified it will be placed in the same folder as the original
If only the path is specified, the new image will be placed in the destination with the same name as the original.
If both the path and image name are specified it will placed in its own destination and given the new name.
Your code should be like this :
if($this->upload->do_upload('file_name')) { $uploadData = $this->upload->data(); $file_name = $uploadData['file_name']; $this->load->library('image_lib'); $configer = array( 'image_library' => 'gd2', 'source_image' => $uploadData['full_path'], 'create_thumb' => TRUE, 'maintain_ratio' => TRUE, 'width' => 950, 'height' => 950, 'new_image' => FCPATH.MEDIA_LIB_URI.'facts/thumbnail/' ); $this->image_lib->clear(); $this->image_lib->initialize($configer); $this->image_lib->resize(); return $file_name; } else { return $this->upload->display_errors(); }
For more : https://www.codeigniter.com/user_guide/libraries/image_lib.html#CI_Image_lib::resize