php - Codeigniter upload library get thumb file name


I can't find any documentation on this. I'm uploading images and using

$config['create_thumb'] = TRUE; 

to create a thumb and preserve the original image.

Is there a way to get the file name of the thumb image?_thumb is automatically added in the name for thumbnails but there's no function to extract the full name.




There really is a much easier way of doing this:

if ($this->upload->do_upload())  // If file was uploaded
    $data = $this->upload->data(); // Returns information about your uploaded file.
    $thumbnail = $data['raw_name'].'_thumb'.$data['file_ext']; // Here it is



CodeIgniter not providing any functions to extract the thumbnail name. It will add _thumb in your filename. If you want to write a custom function to get thumbnail name then use this.

function generate_thumb($filename, $path = '')
    // if path is not given use default path //
    if (!$path) {
        $path = FCPATH . 'somedir' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR;

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

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

    if (!$this->image_lib->resize()) {
        echo $this->image_lib->display_errors();
        return FALSE;
    // get file extension //
    preg_match('/(?<extension>\.\w+)$/im', $filename, $matches);
    $extension = $matches['extension'];
    // thumbnail //
    $thumbnail = preg_replace('/(\.\w+)$/im', '', $filename) . '_thumb' . $extension;
    return $thumbnail;

Input :

echo generate_thumb('someimage.jpg');
echo generate_thumb('other_image.png', FCPATH . 'dirname' . DIRECTORY_SEPARATOR);

Output :


Hope this helps your. Thank you!!




function general_thumbnail_image($source_image_path, $width, $height){

    $thumbnail_config['image_library'] = 'gd2';
    $thumbnail_config['source_image'] = $source_image_path;
    $thumbnail_config['thumb_marker'] = '_'.$width.'x'.$height;
    $thumbnail_config['create_thumb'] = TRUE;
    $thumbnail_config['maintain_ratio'] = TRUE;
    $thumbnail_config['width'] = $width;
    $thumbnail_config['height'] = $height;

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

        $result['status'] = True;

        //If you need complete base path of the thumbnail.
        $result['thumbnail_base_path'] = $this->image_lib->full_dst_path;

        //If you just need name of the thumbnail.
        $source_image_name = $this->image_lib->source_image;
        $extension = strrchr($source_image_name , '.');
        $name = substr($source_image_name , 0, -strlen($extension));
        $result['thumbnail_image_name'] = $name.$config['thumb_marker'].$extension;

        //If you need path similar to source image path.
        $source_image_path = $source_image_path;
        $extension = strrchr($source_image_path , '.');
        $name = substr($source_image_path , 0, -strlen($extension));
        $result['thumbnail_image_path'] = $name.$config['thumb_marker'].$extension;

        $result['status'] = false;
        $result['error'] = $this->image_lib->display_errors('', '');
    return $result;

Calling function.

$thumbnail_result = $this->generate_thumbnail_image('./assests/images/apple.jpg', 180, 180);

Output will look like:

       [status] => 1
       [thumbnail_base_path] => D:/xampp/htdocs/project_name/assets/images/apple_180x180.jpg
       [thumbnail_image_name] => apple_180x180.jpg
       [thumbnail_image_path] => ./assets/images/apple_180x180.jpg 



I think you should create a function to generate the thumb image, like this :

    function generateThumb($fileName) {
    $config['image_library'] = 'gd2';
    $config['source_image'] = './upload/images/clca/' . $fileName;
    $config['create_thumb'] = TRUE;
    $config['maintain_ratio'] = TRUE;
    $config['width'] = 90;
    $config['height'] = 90;
    var file = "";
    $this->load->library('image_lib', $config);
    if(!$this->image_lib->resize()) {
        echo $this->image_lib->display_errors();
    } else {
        // Get File Name
        var file = $fileName."_thumb";

To call that function, firstly you have to upload your file, get the file name & call the function ->$this->generateThumb($file_resp['file_name']);

In your uploaded file folder, there will be 2 images (the original image & the generated thumb image with _thumb suffix)

To get the file name, I used "if" condition to ensure that there is no any errors in generating the thumb image, and then I join the $fileName+"_thumb". I used this way because I've checked the$this->image_lib object but there is no any references to the file name of generated thumb image.


  • Sory, I'm a bit missed your question about getting file name of the thumb file, I've edited my post & the code :)

Hope it helps :)

People are also looking for solutions to the problem: php - MySQL: Wondering how to select records based on day, after adjusting for timezone


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.