Codeigniter Image Manipulation Class Library
Codeigniter Image Manipulation Class Library – We can load image manuipulation class library like this $this->load->library(‘image_lib’);. This library provide various functions that are used for image resizing, thumbnail creation, image cropping, image rotating and image watermarking. It has three major image libraries supported – GD/GD2, NetPBM and ImageMagick. Here in this tutorial, we are going to explain how to use image manipulation class library.
Codeigniter image manipulation class library
Let us understand how image manipulation class library works in codeigniter with examples.
Load image manipulation class library
First load image manipulation class library to use its functions, this library can be loaded simply as below-
How to load image manipulation class library:
$this->load->library('image_lib'); |
Functions:-
There are following functions available in image manipulation class library. Now let us understand one by one with example.
- 1. Processing an image.
- 3. Image watermarking.
1. Processing an image
This method performs resizing, cropping, rotation, or watermarking of image :-
EXAMPLE
Here is simple example of Processing an image function.
Controller code:-
Your controller code now look like this:-
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class image_controller extends CI_Controller { public function index() { $config['image_library'] = 'gd2'; $config['source_image'] = 'uploads/Desert.jpg'; $config['new_image'] = 'imagesManupulation/abcd.jpg'; $config['create_thumb'] = TRUE; $config['maintain_ratio'] = TRUE; $config['width'] = 300; $config['height'] = 500; $this->load->library('image_lib', $config); echo $this->image_lib->resize(); echo $this->image_lib->display_errors(); } }?> |
2. Image watermarking
Two types of watermarking :-
There are two types of watermarking that we can use.
- 1. Text.
- 2. Overlay.
EXAMPLE
Controller code:-
Your controller code now look like this:-
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class image_controller extends CI_Controller { public function watermark() { $this->load->library('image_lib'); $config['image_library'] = 'gd2'; $config['source_image'] = 'uploads/Jellyfish.jpg'; $config['new_image'] = './imagesManupulation/abcd.jpg'; $config['wm_text'] = "Tutorialsplane.com"; $config['wm_type'] = 'text'; $config['wm_font_path'] = './media/arial.ttf'; $config['wm_font_size'] = '16'; $config['wm_font_color'] = '#229954'; $config['wm_font_size'] = '40'; $config['wm_vrt_alignment'] = 'middle'; $config['wm_hor_alignment'] = 'center'; $config['wm_padding'] = '20'; $this->image_lib->initialize($config); $watermark = $this->image_lib->watermark(); echo "Watermark=".$watermark."<br>"; echo $this->image_lib->display_errors(); } } ?> |
Output will be like this.
Class reference:-
There are following references available in image manipulation class library. Now we will explain one by one.
1. Initialize.
This function is used to initialize the class for processing an image.
initialize([$props = array()])
- Parameters.
- $props (array) : Image processing preferences
- Returns : TRUE on success, FALSE in case of invalid settings
- Returns type : Bool
2. Resize.
This is used to resizing the image and create a copy without resizing.
resize()
- Parameters.
- Returns : TRUE on success, FALSE on failure
- Returns type : Bool
3. Crop.
This is used to crop image and set preferences in x and y axis(in pixels).
crop()
- Parameters.
- Returns : TRUE on success, FALSE on failure
- Returns type : Bool
EXAMPLE
Here is simple example of crop class.
Controller code:-
Your controller code now look like this:-
public function crop() { $this->load->library('image_lib'); $config['image_library'] = 'gd2'; $config['source_image'] = 'uploads/Hydrangeas.jpg'; $config['new_image'] = 'imagesManupulation/hydra.jpg'; $config['quality'] = "100%"; $config['maintain_ratio'] = FALSE; $config['width'] = 231; $config['height'] = 154; $config['x_axis'] = '0'; $config['y_axis'] = '0'; $this->image_lib->initialize($config); echo $this->image_lib->crop(); if ( ! $this->image_lib->crop()) { echo $this->image_lib->display_errors(); }} |
4. Rotate.
This method is used to rotate the image.
rotate()
- Parameters.
- Returns : TRUE on success, FALSE on failure
- Returns type : Bool
EXAMPLE
Here is simple example of rotate class.
Controller code:-
Your controller code now look like this:-
public function rotate() { $this->load->library('image_lib'); $config['image_library'] = 'gd2'; $config['source_image'] = 'uploads/Desert.jpg'; $config['rotation_angle'] = 'hor'; $this->image_lib->initialize($config); if ( ! $this->image_lib->rotate()) { echo $this->image_lib->display_errors(); } } |
5. Watermark.
Watermark class is used to set text/overlap watermark over an image.
watermark()
- Parameters.
- Returns : TRUE on success, FALSE on failure
- Returns type : Bool
6. Clear.
Clear method is used to reset all the value used when processing an image.
clear()
- Parameters.
- Returns type : Void
7. Display errors.
This method is used to return all the detected error in string form.
display_errors([$open = '<p>[$close = '</p>']])
- Parameters.
- $open (string) : Error message opening tag
- $close (string) : Error message closing tag
- Returns : Error messages
- Returns type : String
EXAMPLE
Here is simple example of display error class.
Controller code:-
Your controller code now look like this:-
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class image_controller extends CI_Controller { public function errordisplay() { $this->load->library('image_lib'); $config['image_library'] = 'netpbm'; $config['library_path'] = 'localhost/CodeIgniter/application/libraries'; $config['source_image'] = 'uploads/Desert.jpg'; $config['rotation_angle'] = 'hor'; $this->image_lib->initialize($config); if ( ! $this->image_lib->rotate()) { echo $this->image_lib->display_errors(); }}} ?> |
Advertisements