How to create multiple thumbnails with codeigniter?

by lindsey.homenick , in category: PHP Frameworks , 9 months ago

How to create multiple thumbnails with codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by muriel.schmidt , 8 months ago

@lindsey.homenick 

To create multiple thumbnails using CodeIgniter, you can use the image manipulation class provided by CodeIgniter. Here's a step-by-step guide on how to create multiple thumbnails in CodeIgniter:

  1. Load the image library in your controller:


First, load the image manipulation library in your controller by adding the following line of code in the constructor function:

1
$this->load->library('image_lib');


  1. Create a function to generate multiple thumbnails:


Next, create a function in your controller to generate multiple thumbnails. Here's an example of how you can create multiple thumbnails of different sizes:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public function create_thumbnails($image_path, $filename) {
  $config['image_library'] = 'gd2';
  $config['source_image'] = $image_path;
  $config['create_thumb'] = TRUE;
  $config['maintain_ratio'] = TRUE;
  
  // Thumbnail 1
  $config['width'] = 100;
  $config['height'] = 100;
  $config['new_image'] = 'path_to_save_thumbnail1/' . $filename;
  
  $this->image_lib->initialize($config);
  $this->image_lib->resize();
  $this->image_lib->clear();
  
  // Thumbnail 2
  $config['width'] = 200;
  $config['height'] = 200;
  $config['new_image'] = 'path_to_save_thumbnail2/' . $filename;
  
  $this->image_lib->initialize($config);
  $this->image_lib->resize();
  $this->image_lib->clear();
}


In this function, we have defined two different thumbnails of sizes 100x100 and 200x200. You can add more thumbnails by adding additional configuration arrays and calling the initialize() and resize() methods for each thumbnail.

  1. Call the function to generate thumbnails:


Finally, call the create_thumbnails() function in your controller by passing the image path and filename as parameters:

1
2
$image_path = 'path_to_original_image/' . $filename;
$this->create_thumbnails($image_path, $filename);


This will generate multiple thumbnails of the original image specified by $image_path and save them in the specified directories with the given filenames.


By following these steps, you can easily create multiple thumbnails in CodeIgniter using the image manipulation library.