Tutorials

Article Content:Generate image thumbnails on the fly in codeigniter

Generate image thumbnails on the fly in codeigniter

Generate image thumbnails on the fly in codeigniter.

How do you do my friends, I hope you will be good, I came again with new important tutorial for any web developer, so let's go

In the last days I had been busy to figure out the best way to generate thumbnails for image module in my own Codeigniter CMS, but the problem was: how to do it simple and flexible?

After some googling if figure out there was a good way to create thumbnails, it was called  'thumbnails on the fly', to simplify this idea more you just suppose that your client ask you to build news website

then as many news website you will have main page which fill with many short news feeds like this, so in the admin panel you will create form to add latest news, I suppose you fields will be like this :

1- News title

2- News subject

3- News tags

4- News image

You will upload your image only once, and it will be resized and created as small copies, the original will display on the news content page and the small copies will be on the home page to reduce bandwidth consuming, also this will increase site performance, because pages will load faster

After some days your customer come again to renew website design.so you start to replace design files and ooops, you have a problem, what about thumbs?

You should replace old thumbs with new thumbs because the new design changed and already thumb sizes changes, you will do it by looping on the old images to delete it, then looping on the original images to generate new thumbs with new sizes dimensions.

 

The solution

To solve this problem we need to create helper function which will give us a beautiful short solution to generate thumbnails on the fly, to do this just copy this function and put it in your custom helper

/**
 * Thumb()
 * A TimThumb-style function to generate image thumbnails on the fly.
 *
 * @access public
 * @param string $fullname
 * @param int $width
 * @param int $height
 * @param string $image_thumb
 * @return String
 *
 */
    function thumb($fullname, $width, $height)
    {
        // Path to image thumbnail in your root
        $dir = './path/to/uploads/';
        $url = base_url() . 'path/to/uploads/';
        // Get the CodeIgniter super object
        $CI = &get_instance();
        // get src file's extension and file name
        $extension = pathinfo($fullname, PATHINFO_EXTENSION);
        $filename = pathinfo($fullname, PATHINFO_FILENAME);
        $image_org = $dir . $filename . "." . $extension;
        $image_thumb = $dir . $filename . "-" . $height . '_' . $width . "." . $extension;
        $image_returned = $url . $filename . "-" . $height . '_' . $width . "." . $extension;

        if (!file_exists($image_thumb)) {
            // LOAD LIBRARY
            $CI->load->library('image_lib');
            // CONFIGURE IMAGE LIBRARY
            $config['source_image'] = $image_org;
            $config['new_image'] = $image_thumb;
            $config['width'] = $width;
            $config['height'] = $height;
            $CI->image_lib->initialize($config);
            $CI->image_lib->resize();
            $CI->image_lib->clear();
        }
        return $image_returned;
    }

Then you can use it in your html code like this, the first parameter will be only your image name like this, the second was width and the third was height

<img src="<?= thumb('picture.jpg', 200, 100); ?>" class="md-img" alt="" >
 

The original reference: https://github.com/hernantz/Codeigniter-Thumbnail-Generator



Leave a Reply

Your email address will not be published.


Notify me of followup comments via e-mail.
You can also Subscribe without commenting.