Google trends Public Holidays Coupon Code Code Compiler

Upload Image and Create Thumbnail in CodeIgniter

Oct 12, 2023

Upload Image and Create Thumbnail in CodeIgniter

Upload Image and Create Thumbnail in CodeIgniter - A comprehensive guide on how to implement image uploads and thumbnail generation in CodeIgniter, complete with example code and best practices for web development.

Uploading images and creating thumbnails is a common requirement in web development. In this guide, we'll explore how to implement these features in CodeIgniter, a popular PHP framework. Image uploads and thumbnail generation are essential for various web applications, including e-commerce sites, social media platforms, and content management systems.


Step 1: Create an Upload Form

Start by creating an HTML form that allows users to upload images. In your view file (e.g., upload_form.php), include a simple HTML form with an input field for selecting an image file and a submit button.

<form method="post" acti enctype="multipart/form-data">
    <input type="file" name="userfile" />
    <input type="submit" name="submit" value="Upload" />

In this form, we set the enctype attribute to "multipart/form-data" to enable file uploads.

Step 2: Upload the Image

In your CodeIgniter controller, handle the image upload process. Use the 'upload' library to manage file uploads. Before using the library, make sure you have configured the upload settings in config/upload.php.

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);

if ($this->upload->do_upload('userfile')) {
    // File uploaded successfully
} else {
    // Upload failed, handle errors

In this code:

  • We specify the upload path (where the uploaded files will be stored) and the allowed file types.
  • We load the 'upload' library with the provided configuration.
  • If the upload is successful, you can proceed with other operations.

Step 3: Create Thumbnails

To generate thumbnails, you can use the 'image_lib' library provided by CodeIgniter. Configure the settings for thumbnail creation and apply them to the uploaded image.

$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/' . $this->upload->data('file_name');
$config['new_image'] = './uploads/thumbnails/';
$config['create_thumb'] = true;
$config['maintain_ratio'] = true;
$config['width'] = 100;
$config['height'] = 100;
$this->load->library('image_lib', $config);


In this code:

  • We specify the image library to use (e.g., 'gd2').
  • We define the source image (the uploaded file) and the location for the generated thumbnail.
  • We set options like creating a thumbnail, maintaining the aspect ratio, and specifying the width and height.
  • The 'image_lib' library is loaded with the configuration, and the resize() function is called to create the thumbnail.


Implementing image uploads and thumbnail generation in CodeIgniter enhances the capabilities of your web applications. Users can upload images, and you can create thumbnails to improve loading times and user experiences. By following this guide and integrating these features into your projects, you'll have the tools to manage images effectively and enhance user interactions.

Copyright 2024. All rights are reserved