
Upload, crop, re-size, etc, are the basic image customization that most websites or applications need to have. If we start implementing with native PHP support, then this functionality can be pretty big and (maybe) buggy. Here comes the role of an application framework. Codeigniter image manipulation library provides an effortless and efficient way to provide this kind of feature, which requires the least effort from developers. If you are a Codeigniter developer, you should take advantage of the blessings of ready-made functionality. Let’s have a look at these image modification techniques of the Codeigniter framework today. Hopefully, we will be able to save our development time and have a bug-free, robust implementation.
Read The Complete CodeIgniter Tutorials Series By CodeSamplez.com
Upload An Image With CodeIgniter:
I strongly recommend following the codeigniter file upload tutorial first. Here, you will see how we can easily upload a file inside a codeigniter application. The only extra concern when uploading an image is setting the configuration file properly. The following properties are essential parts of image-type file upload:
- max_height: Indicates the maximum height of the image file that is allowed to be uploaded.
- max_width: Indicates the maximum height of the image file that is allowed to be uploaded.
- max_size: Indicates the maximum size of the image file that is allowed to be uploaded.
- allowed_types: Define the type of images that are allowed to be uploaded.
CodeIgniter Image Manipulation Library Configuration:
There are a few settings which can be used commonly for all available operations. Those are given below:
- image_library: We can specify which image library to be used. Codeigniter supports GD, GD2, ImageMagic and NetPBM at this moment.
- source_image: The image to be processed.
- new_image: you may not want to change the original image and create a new version of the modified one. In this case, use this configuration to specify the new image file’s full path/name.
Additionally, For our tutorial, we will be using the following common configuration loading code snippet:
$image_data = $this->upload->data();
$config['manipulation_type']['source_image'] = $image_data['full_path'];
$this->load->library('image_lib', $config['manipulation_type']);
Code language: PHP (php)
This is to be placed after the image file upload, where it takes the image data from “$this->upload->data()” method. Lets get into action.
Re-size An Image:
A typical configuration for resizing an image would be as follows:
$config['resize'] = array(
'image_library' => 'gd2',
'maintain_ratio' => TRUE,
'width' => 250,
'height' => 250,
);
Code language: PHP (php)
The ‘maintain_ratio’ property tells the library that the original ratio must need to be maintained while resizing if set to ‘true.’
On the controller, after the upload functionality, use the code workflow below:
//load/initialize the image library with resize specific configuration
$this->image_lib->resize();
Code language: PHP (php)
Wow! See how easy it is? We don’t need to get into all the raw details of the task. Setting the configuration and calling the method does our work very fine. The Rest of the operations are also as simple as this. Keep reading.
Crop An Image:
Let’s use a sample configuration file to crop an image that is much similar to re-size configuration:
$config['crop'] = array(
'image_library' => 'gd2',
'maintain_ratio' => FALSE,
'width' => 250,
'height' => 250,
);
Code language: PHP (php)
Now, on the controller, use the code workflow as below. This is almost similar to the re-size functionality, except the main ‘resize()’ method is replaced by the ‘crop()’ method.
//load/initialize the codeigniter image manipulation library with crop specific configuration
$this->image_lib->crop();
Code language: PHP (php)
Rotate An Image:
Well, as the functionality indicates, we will provide the rotation angle, and how much the image will be rotated. Let’s use the configuration as below:
$config['rotate'] = array(
'image_library' => 'gd2',
'rotation_angle' => 90
);
Code language: PHP (php)
Similar to earlier implementations, the same workflow will do the work here as well. Except for the core method call:
[php]
//load/initialize the codeigniter image library with rotate specific configuration
$this->image_lib->rotate();
[/php]
Code language: PHP (php)
Water-Mark An Image:
To watermark an image, two necessary settings are the type of watermark(wm_type) and the watermark content (‘wm_text’ if type text, ‘wm_overlay_path’ if type image). There are many settings for this feature, but for now, the following settings will be enough to create a text watermark:
$config['watermark'] = array(
'image_library' => 'gd2',
'wm_text' => 'Copyright 2013 - CodeSamplez.com',
'wm_type' => 'text'
);
Code language: PHP (php)
After uploading the image file and codeigniter image manipulation library loaded with the correct configuration, the following code on the controller will do the work of the watermark properly:
//load/initialize the image library with watermark specific configuration
$this->image_lib->watermark();
Code language: PHP (php)
Here is a screenshot of how the demo application would look in rotation mode selected:
Further References:
Read the official image library documentation for in-depth details of the configuration settings. Please let me know if you are having any issues/comments. Happy coding 🙂
Discover more from CODESAMPLEZ.COM
Subscribe to get the latest posts sent to your email.
how to display image without folder and should directly come from database
You can do it by using a ‘blob’ type column in database table. Retrieval should be similar like other regular columns. For saving, you can refer to my codeigniter file upload tutorial. However, I don’t recommend this technique as this will make your database server slow.
How about if we using type blob, is there heavy to load it ?
It was very very useful to me, thank you!
how I can do round image ?
image can’t be made round by processing. It will always be an rectanglular. You can show it on web page as like rounded by applying some css. Simply google it.
Nice post, Keep sharing
Hi Friends,
In Codeigniter normally we load library $this->load->library(“libname”); so here we using $this->load->library(“app/uploader”); so is it this external library or default library.