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.