Course Content
Introduction to CodeIgniter
CodeIgniter is a powerful PHP framework built for developers who need a simple and elegant toolkit to create full-featured web applications.
0/3
MVC (Model-View-Controller)
MVC stands for Model-View-Controller. MVC is an application design model consisting of three interconnected parts. They include the model (data), the view (user interface), and the controller (processes that handle input).
0/6
Sessions
The Session class allows you to maintain a user’s "state" and track their activity while they browse your site.
0/1
URI Routing
There is a one-to-one relationship between a URL string and its corresponding controller class/method. The segments in a URI normally follow this pattern:
0/1
Forms and Input
Forms provide a way for users to interact with the application and submit data.
0/1
Composer
Composer is dependency manager in PHP. it allows you to declare the libraries your project depends on and it will manage (install/update) them for you.
0/1
Security
You can enable CSRF protection by modifying your application/config/config.php file
0/1
Working with Database
Like any other framework, we need to interact with the database very often and CodeIgniter makes this job easy for us. It provides a rich set of functionalities to interact with the database.
0/5
DataTable
DataTables is a table enhancing plug-in for the jQuery Javascript library that helps in adding sorting, paging, and filtering abilities to plain HTML tables with minimal effort. The main goal is to enhance the accessibility of data in normal HTML tables.
0/1
Spreadsheet
PhpSpreadsheet is a PHP library for reading and writing spreadsheet files. Importing Excel and CSV into MySQL help to save the user time and avoid repetitive work.
0/1
Payment Gateway
Razorpay and PayTM Payment Gateway
0/2
Chatbot
WhatsApp Chatbot and Telegram Chatbot
0/2
CodeIgniter 3
About Lesson

On the website, we can add images to the web page without adding the link to the external image file. Let’s make it easy, the DATA URIs is the way where we can just add the data of the image into the page and it works.

 

PHP code to Convert Image to DATA URI

<?php 
	// upload image and get it's metadata.
    $data 		= $this->upload->data();
    $file_name 	= $data['file_name'];
    $image 	    = base_url($path.$file_name);
    $file 		= file_get_contents($image);
    $file_type 	= $data['file_type'];
    $uri 		= 'data: '.$file_type."; base64, ".base64_encode($file);
?>

In this tutorial, we will learn how to create the DATA URI from the Image using PHP CodeIgniter, let’s start with the first to create views/index.php along with the user interface.

1. Create an index.php file in applications/views/ directory

<!DOCTYPE html>
<html lang="en">

<head>
	<title>Upload Image</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" 
	href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
	<script 
	src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
	</script>
	<script 
	src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js">
	</script>
	<script 
	src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js">
	</script>
</head>

<body>
	<div class="container mt-4">
		<h3>Upload Image</h3>
		<form action="<?php echo base_url('upload'); ?>" 
		method="post" enctype="multipart/form-data">
			<div class="form-group">
				<label for="email">Image:</label>
				<input type="file" class="form-control form-control-sm" 
				id="file" name="file" required>
			</div>
			<button type="submit" 
			class="btn btn-sm btn-primary">Upload</button>
		</form>
	</div>
</body>

</html>

2.  Create a controller file Welcome.php in the applications/controllers/ directory.

<<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

	public function index() {
		$this->load->view('add');
	}

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

        if (!$this->upload->do_upload('file')) {
        	$error = array('error' => $this->upload->display_errors());
        	echo "<pre>";
        	print_r($error);
        } else {
        	$data 		= $this->upload->data();
        	$file_name 	= $data['file_name'];
        	$image 		= base_url($path.$file_name);
        	$file 		= file_get_contents($image);
        	$file_type 	= $data['file_type'];
        	$uri 		= 'data: '.$file_type."; base64, ".base64_encode($file);
        	echo '<img src="'.$uri.'" />';
        	unlink($path.'/'.$file_name);
        }
	}
}
?>

3. Add the following lines in config/routes.php

$route['upload']	= 'welcome/upload';

4. Add the following lines in config/autoload.php

autoload['libraries'] = array('session', 'form_validation');
$autoload['helper'] = array('url', 'form', 'date', 'file', 'download', 'cookie');

We are done and you can test your application.