[eluser]ColinHoernig[/eluser]
Say I have a upload form that resizes the original uploaded image and also creates a thumbnail of the same image. If I upload a file that has the name "image name here.gif" it uploads as "image_name_here.gif". How would I access the newly created images in order to insert their urls (not full, just the filename ex: "image_name_here.gif") into a database.
Right now I'm doing it like this:
do_upload method
Code:
function _do_upload($field_name) {
$config['upload_path'] = './img/products/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '1600';
$config['max_height'] = '1200';
$this->load->library('upload', $config);
if(!$this->upload->do_upload($field_name)) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('administration/products/add_product', $error);
} else {
$data = array('upload_data' => $this->upload->data());
$temp = $this->upload->data();
$config['image_library'] = 'GD2';
$config['source_image'] = './img/products/' . $temp['file_name'];
$config['maintain_ratio'] = TRUE;
$config['create_thumb'] = TRUE;
$config['quality'] = '80';
$config['height'] = '100';
$config['width'] = '100';
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->clear();
$config['create_thumb'] = FALSE;
$config['height'] = '250';
$config['width'] = '250';
$this->image_lib->initialize($config);
$this->image_lib->resize();
$data = array('upload_data' => $this->upload->data());
return $data;
}
}
and part of the add product/upload method
Code:
$upload = $this->_do_upload('product_image');
if($upload != NULL) {
$img_url = str_replace(" ", "_", $_FILES['product_image']['name']);
$product_image_url = str_replace("'", "", $img_url);
$pos1 = strpos($product_image_url, ".");
$file_type = substr($product_image_url, $pos1);
$product_thumb_url = substr($product_image_url, 0, $pos1);
$product_thumb_url .= "_thumb";
$product_thumb_url .= $file_type;
I know this isn't the way to do it, because there must be a way to access the new data from the resized and thumbnailed files, rather than using the actual filename from the uploaded file (because image_lib library changes it).
The way I'm doing it now doesn't work for files with spaces in them, or files that have extra periods in them.
Say I upload a file called "image name 1.0 here.gif", input into the database would be "image_name_1_thumb.0_here.gif" which isn't what I need, it needs to be "image_name_1.0_here_thumb.gif".
Any help would be greatly appreciated.