Image Manipulation Library in CodeIgniter

CodeIgniter Provides an Image Manipulation Library to manipulate images. Sometimes there is a need to resize, crop, watermark our images while we upload then this library plays an important role in this work. It is also called the GD library.

CodeIgniter provides the following functions in Image Manipulation Library:

  1. $this->image_lib->resize()
  2. $this->image_lib->rotate()
  3. $this->image_lib->crop()
  4. $this->image_lib->watermark()
 


1. $this->image_lib->resize():
this method is used to resize an image you can give the width and height whatever you want also you can manage the ratio of the image by enabling maintain_ratio: TRUE.

Example:

 $data = $_FILES['image']['name'];  
            $config['image_library'] = 'gd2';
            $config['source_image'] = $_FILES['image']['tmp_name'];
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = TRUE;
            $config['width'] = 300;
            $config['height'] = 300;
            $config['new_image'] = 'uploads/' . $data;
            $this->load->library('image_lib', $config);
            $this->image_lib->resize();



2. $this->image_lib->rotate():
this method is used to rotate an image. Sometimes some pictures clicked by the wrong angle so the image got rotated. To fix this problem we use this method.

Example:


            $data = $_FILES['image']['name'];  
            $config['image_library'] = 'gd2';
            $config['source_image'] = $_FILES['image']['tmp_name'];
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = TRUE;
            $config['rotation_angle'] = ‘90’;
            $config['width'] = 300;
            $config['height'] = 300;
            $config['new_image'] = 'uploads/' . $data;
            $this->load->library('image_lib', $config);
            $this->image_lib->rotate();
 



3. $this->image_lib->crop():
this method is used to crop an image. Sometimes there is a need to crop a little bit part of an image then we can do this by using this method.

Example:


            $data = $_FILES['image']['name'];  
            $config['image_library'] = 'gd2';
            $config['source_image'] = $_FILES['image']['tmp_name'];
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = TRUE;
            $config['x_axis'] = ‘150’;
            $config['width'] = 300;
            $config['height'] = 300;
            $config['new_image'] = 'uploads/' . $data;
            $this->load->library('image_lib', $config);
            $this->image_lib->crop();
 



4. $this->image_lib->watermark():  
this method is used to add watermark on image. Sometimes we want to apply a watermark on an image to protect from copying an image.

Example:


            $data = $_FILES['image']['name'];  
            $config['image_library'] = 'gd2';
            $config['source_image'] = $_FILES['image']['tmp_name'];
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = TRUE;
            $config['x_axis'] = ‘150’;
            $config['width'] = 300;
            $config['height'] = 300;
            $config['wm_text'] = 'Codeigniter @2018 – Web Development Institue';
            $config['wm_type'] = 'text';
            $config['wm_font_size'] = '32';
          $config['wm_font_color'] = '#4f1a10';
 $config['wm_vrt_alignment'] = 'middle';
 $config['wm_hor_alignment'] = 'center';
 $config['wm_padding'] = '5';
 $config['wm_margin'] = '20';         
            $config['new_image'] = 'uploads/' . $data;
            $this->load->library('image_lib', $config);
            $this->image_lib->crop();
 


How to  Use Image Manipulation Library:

Now we see the following steps on how we can use this library on our project. In this example, we are using only resize() method you can use another method by just changing the controller code. Let’s see the following steps:


Step1:  
Write the following code in your default view file: welcome_message


<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
            <meta charset="utf-8">
            <title>GD Library</title>
 
</head>
<body>
 
<h1>GD Library</h1>
 
<form method="post" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" name="submit">
</form>
 
 
 
 
</body>
</html>
 


Step2:  
Write the following code in your default controller file: Welcome.php


<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 
class Welcome extends CI_Controller {
 
            /**
             * Index Page for this controller.
             *
             * Maps to the following URL
             *                      http://example.com/index.php/welcome
             *          - or -
             *                      http://example.com/index.php/welcome/index
             *          - or -
             * Since this controller is set as the default controller in
             * config/routes.php, it's displayed at http://example.com/
             *
             * So any other public methods not prefixed with an underscore will
             * map to /index.php/welcome/<method_name>
             * @see https://codeigniter.com/user_guide/general/urls.html
             */
             
             
           
       //public $is_hookable = TRUE;
  
             
            public function index()
            {
                        if(isset($_POST['submit']))
                        {
                                   
                                    $data = $_FILES['image']['name'];
            $config['image_library'] = 'gd2';
            $config['source_image'] = $_FILES['image']['tmp_name'];
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = TRUE;
            $config['width'] = 300;
            $config['height'] = 300;
            $config['new_image'] = 'uploads/' . $data;
            $this->load->library('image_lib', $config);
            $this->image_lib->resize();
            $img = '<img src="' . base_url() . 'uploads/' . $data . '">';
         echo json_encode(array('img' => 'file uploaded successfully'));
                                   
                                   
                        }          
                                   
                        $this->load->view('welcome_message');
            }
}


Step3:  
To upload an image on the directory you must create directory name uploads in the root directory of your project.

Categories: web development codeigniter

Trending Courses

CodeIgniter

Regular : 45 Days

Fastrack : 20 Days

Crash : 10 Days

Advance Digital Marketing

Regular : 6 Months

Fastrack : 3 Months

Crash : 2 Months

React JS

Regular : 45 Days

Fastrack : 20 Days

Crash : 7 Days

Laravel

Regular : 45 Days

Fastrack : 20 Days

Crash : 10 Days

Front End Developer

Regular : 7 Month

Fastrack : 4 Month

Crash : 2 Month

Related Blogs

Request For Demo