Codeigniter 4 Image Size Compress Example

Codeigniter compress image size. In this tutorial, you will learn how to upload and compress image size in codeigniter app.

This tutorial will guide you step by step on how to compress image size and upload in codeigniter app. And you can easily compress any image type like png, jpg, jpeg, gif etc.

How to Compress Image Size In CodeIgniter

Follow the following steps and you can easily compress image size in codeigniter app:

  • Download Codeigniter Latest
  • Basic Configurations
  • Create Database With Table
  • Setup Database Credentials
  • Create Controller
  • Create Views
  • Start Development server

Step 1: Download Codeigniter Project

In this step, you will download the latest version of Codeigniter 4, Go to this link https://codeigniter.com/download Download Codeigniter 4 fresh new setup and unzip the setup in your local system xampp/htdocs/ . And change the download folder name “demo”

Step 2: Basic Configurations

Next, you will set some basic configuration on the app/config/app.php file, so let’s go to application/config/config.php and open this file on text editor.

Set Base URL like this

public $baseURL = 'http://localhost:8080';
To
public $baseURL = 'http://localhost/demo/';

Step 3: Create Database With Table

In this step, you need to create a database name demo, so open PHPMyAdmin and create the database with the name demo. After successfully create a database, you can use the below SQL query for creating a crop_images table in your database.

CREATE TABLE crop_images ( id int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary Key', title varchar(100) NOT NULL COMMENT 'Title', created_at varchar(20) NOT NULL COMMENT 'Created date', PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='demo table' AUTO_INCREMENT=1;

Step 4: Setup Database Credentials

In this step, you need to connect our project to the database. So, visit app/Config/Database.php and open database.php file in text editor. After opening the file in a text editor, YOU need to set up database credentials in this file like below.

	public $default = [ 'DSN' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'demo', 'DBDriver' => 'MySQLi', 'DBPrefix' => '', 'pConnect' => false, 'DBDebug' => (ENVIRONMENT !== 'production'), 'cacheOn' => false, 'cacheDir' => '', 'charset' => 'utf8', 'DBCollat' => 'utf8_general_ci', 'swapPre' => '', 'encrypt' => false, 'compress' => false, 'strictOn' => false, 'failover' => [], 'port' => 3306, ];

Step 5: Create Controller

In this step, Go to app/Controllers and create a controller name ImageUpload.php. Then add the following code into it:

<?php class ImageUpload extends CI_Controller { /** * Manage __construct * * @return Response */ public function __construct() { parent::__construct(); $this->load->helper(array('form', 'url')); } /** * Manage index * * @return Response */ public function index() { $this->load->view('imageUploadForm', array('error' => '' )); } /** * Manage uploadImage * * @return Response */ public function uploadImage() { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = 1024; $this->load->library('upload', $config); if ( ! $this->upload->do_upload('image')) { $error = array('error' => $this->upload->display_errors()); $this->load->view('imageUploadForm', $error); }else { $uploadedImage = $this->upload->data(); $this->resizeImage($uploadedImage['file_name']); print_r('Image Uploaded Successfully.'); exit; } } /** * Manage uploadImage * * @return Response */ public function resizeImage($filename) { $source_path = $_SERVER['DOCUMENT_ROOT'] . '/uploads/' . $filename; $target_path = $_SERVER['DOCUMENT_ROOT'] . '/uploads/'; $config_manip = array( 'image_library' => 'gd2', 'source_image' => $source_path, 'new_image' => $target_path, 'maintain_ratio' => TRUE, 'width' => 500, ); $this->load->library('image_lib', $config_manip); if (!$this->image_lib->resize()) { echo $this->image_lib->display_errors(); } $this->image_lib->clear(); }
} ?>

Step 6: Create Views

In this step, you need to create imageUploadForm.php, go to application/views/ directory and create imageUploadFormphp file. and update the following HTML into your files:

<!DOCTYPE html>
<html> <head> <title>Codeigniter Compress Image Size Example - Tutsmake.com</title> </head> <body> <?php echo $error;?> <form method='post' action='/image-upload/post' enctype='multipart/form-data'> <input type="file" name="image" size="20" /> <input type="submit" value="upload" /> </form> </body>
</html>

Step 7: Start Development server

For start development server, open your terminal and execute the following command it:

php spark serve

Then, Go to the browser and hit below the URL:

http://localhost:8080

Conclusion

Codeigniter compress image size. In this tutorial, you have learned how to upload and compress image size in codeigniter app.

Recommended CodeIgniter Tutorials

If you have any questions or thoughts to share, use the comment form below to reach us.

Related Posts