How to Create Thumbnail with CodeIgnitor

Uploading images is a common task in almost every websites. Only displaying the original images will make the site load slower. That’s why we need to create a thumbnail for each image and show it by default.

CodeIgnitor provides a built-in class called Image Manipulation to make creating thumbnail easy to complete.

Image Manipulation class allows you to perform the following actions:

  • Resizing
  • Thumbnail Creation
  • Cropping
  • Rotating
  • Watermarking
$image = '.upload/gallery/test_image_001.jpg'

$config_manip = array(
    'image_library' => 'gd2',
    'source_image' => $image,
    'new_image' => $image,
    'maintain_ratio' => TRUE,
    'create_thumb' => TRUE,
    'thumb_marker' => '_thumbnaul',
    'width' => 150,
    'height' => 150,
);

$this->load->library('image_lib');
$this->image_lib->initialize($config_manip);
$this->image_lib->resize();      
$this->image_lib->clear();

This function will create a 150×150 thumbnail called test_image_001_thumbnail.jpg in ./upload/gallery/.

All available parameters of Image Manipulation can be found on https://www.codeigniter.com/userguide3/libraries/image_lib.html.

Leave a Comment

Your email address will not be published. Required fields are marked *

We care about your privacy and data security. Google serves cookies to analyse traffic and show ads on this site. By using this site you agree to these cookies being set. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close