I am trying to make a simple app with Codeigniter where i need to store thumbnail image path in MySql database. The images along with the thumbnail versions upload correctly in the upload folder and I can store the image path in my database while uploading but i can not store the thumbnail image path in the database. I tried this in the controller:
$thumbnail= $this->resize($data['upload_data']['full_path'], $data['upload_data'] ['file_name']);
$thumbnail_path = './uploads/'. $thumbnail;
$newRow = array(
"title" => $this->input->post('title'),
"img_path" => $imgpath,
"thumbnail_path" => $thumbnail_path,
"post_body" => $this->input->post('post_body')
)
But it only stores "./uploads/" in the thumbnail_path column in my table, not the actual thumbnail path. Where i expect it to store something like "./uploads/Lighthouse_thumb.jpg". I can not figure out how to get the path of the thumbnail images to store them in the database. Here is my full upload controller:
class Upload extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('form');
}
function index() {
$this->load->view('upload_form', array('error'=>''));
}
function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
} else {
$image_data = $this->upload->data();
$imgpath = './uploads/' . $image_data['file_name'];
$data = array('upload_data'=>$this->upload->data());
$thumbnail= $this->resize($data['upload_data']['full_path'], $data['upload_data']['file_name']);
$thumbnail_path = './uploads/'. $thumbnail;
$newRow = array(
"title" => $this->input->post('title'),
"img_path" => $imgpath,
"thumbnail_path" => $thumbnail_path,
"post_body" => $this->input->post('post_body')
);
$this->load->model('postimage_path');
$this->postimage_path->insert($newRow);
$this->load->view('upload_success', $data);
}
}
function resize($path, $file) {
$config['image_library'] = 'gd2';
$config['source_image'] = $path;
$config['create_thumb'] = TRUE;
$config['maintian_ratio'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;
$config['new_image'] = './uploads/' . $file;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
}
$thumbnail_path = './uploads/'. $thumbnail;
So thumbnail_path
contains only "./uploads/" : that tells us that $thumbnail
is false (or null or empty string etc.), meaning that the return value of the call to resize is wrong - it seems you expect it to return the file name.
function resize($path, $file) {
$config['image_library'] = 'gd2';
$config['source_image'] = $path;
$config['create_thumb'] = TRUE;
$config['maintian_ratio'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;
$config['new_image'] = './uploads/' . $file;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
resize
returns nothing! And you assign that nothing to $thumbnail_path
. That's your problem.
You seem to have simply copied the code from the CodeIgniter docs. From said docs (emphasis mine):
The above code tells the image_resize function to look for an image called mypic.jpg located in the source_image folder, then create a thumbnail that is 75 X 50 pixels using the GD2 image_library. Since the maintain_ratio option is enabled, the thumb will be as close to the target width and height as possible while preserving the original aspect ratio. The thumbnail will be called mypic_thumb.jpg
So there you have it! If your filename is $data['upload_data']['file_name']
, your thumbnail will be $data['upload_data']['file_name'] . "_thumb"
. Have a look at your file system, the files should be there for you to see.
So to fix your problem, this should do the trick:
$pathinfo = pathinfo($data['upload_data']['file_name']);
$thumbnail_path = './uploads/'. $data['upload_data']['file_name'] . "_thumb" . $pathinfo['extension'];