Codeigniter Create Helper: Codeigniter comes with 20+ built in helpers. Which has tons of functionalities that we can use in our website development. Still there may be time when our requirements do not match with any of available functions. So we have to create our own helpers. Codeigniter Helpers are just collection of related functions wrapped inside a file. So that whenever we need it we can load only that particular helper.
CodeIgniter Create Helper from scratch
Codeigniter create helper is as easy as creating a simple php function. Just have to follow few steps for codeigniter framework. For demo purpose we are creating a function to encode image using base64 method. If you don’t know what is base64 image encoding please refer to Base64 Images Advantages & Disadvantages. Lot’s of request for Base64 encode image for php comes from our visitors. So I thought may be I can integrate it with codeigniter.
Step 1. Create a new file called myimage_helper.php inside application/helpers directory. Codeigniter follows a pattern for helper files. Which is <helper_name>_helper.php.
Step 2. Write following code inside myimage_helper.php file that we just created.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); if ( ! function_exists('convertToBase64')) { function convertToBase64($path) { $path = FCPATH.$path; $type = pathinfo($path, PATHINFO_EXTENSION); $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); return $base64; } } |
First we check for convertToBase64 function already exists or not. If not then we’ll create a new one. ConvertToBase64 accepts 1 parameter which is $path (location of image file). Next step is to get information of file specified in parameter by using pathinfo function. file_get_contents function will retrieve data from specified file and stores in $data variable. Now the main conversion part comes where we encode image using base64 method. Base64_encode function is already in php to perform that part. Final step is to return base64 converted string in correct format.
Step 3. Load newly created helper in controller __construct or in a method where needed.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Home extends CI_Controller { function __construct() { parent::__construct(); $this->load->helper("myimage"); } public function index() { $this->load->view('home'); } } |
Step 4. Now add view code in views/home.php file or wherever you want to output base64 image.
1 2 3 |
<div> <img src="<?= convertToBase64('public/coderiddles.jpg') ?>" alt="Base64 Encoded Image"/> </div> |
As you can see, we used new helper method.
Step 5. Now navigate to home controller’s index method. For me it’s http://localhost:8888/CI/index.php/home. You will get your image in browser but i will be in base64 encoded just like below.
As you can see, image content is encoded & integrated with HTML code. So no more separate request need for image loading. For Advantages & Disadvantages of doing so please refer to Base64 Images Advantages & Disadvantages.
Our new helper is working and it also outputs image. we can add as many function we want to inside single helper but it is recommended you categorise those functions via multiple helpers.
We finished Codeigniter Create helper Tutorial. If you find it useful for Codeigniter create helper tutorial, please hit like & leave a comment for us. If you have any issue please feel free to mention same in comment so we can serve you better. Visit & like our Facebook Fan Page to get instant update of our website.
Pingback: Base64 Images Advantages & Disadvantages » CodeRiddles
Pingback: Codeigniter Create Helper Tutorial - CodeRiddle...