Image Resize, Crop, Add Watermark and Upload in Codeigniter


Posted on 2018-10-11 07:05:45

Download Source Code / File

Image Resize, Crop, Add Watermark and Upload in Codeigniter easy way. It takes only few seconds. Click on the download button to download the source code.



**view**

<form action="welcome/imageedit" method="post" enctype="multipart/form-data">
<input type="file" name="editimage">
<input type="submit" value="submit">
</form>           
            
        
**controller**            
 
defined('basepath') or exit('no direct script access allowed');

class welcome extends ci_controller {
    
    function __construct() {
        parent::__construct();
        
        $this->load->library('upload');
        $this->load->library('image_lib');
        
    }

	public function index()
	{
		$this->load->view('welcome_message');
	}
        
        function imageedit() {
            
        $config['upload_path'] = './images/product/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['file_name'] = str_replace('_', '', str_replace(' ', '', strtolower($_files['editimage']['name'])));
        $this->upload->initialize($config);
        $this->upload->do_upload('editimage');
        $data = array(
            'image' => $config['file_name']
        );

        $path = './images/product/' . str_replace('_', '', str_replace(' ', '', strtolower($_files['editimage']['name'])));

        $upload_data = $this->upload->data();
        $upload_img_data = getimagesize($upload_data['full_path']);
        $water_mark = "";
        $configrez['image_library'] = 'gd2';
        $configrez['source_image'] = $path;
        $configrez['create_thumb'] = false;
        $configrez['maintain_ratio'] = false;
        
        if ($upload_img_data[0] > $upload_img_data[1]) {
            
            $configrez['width'] = $upload_img_data[1];
            $configrez['height'] = $upload_img_data[1];
            $configrez['x_axis'] = ($upload_img_data[0] - $upload_img_data[1]) / 2;
            $configrez['y_axis'] = 0;
            $water_mark = $upload_img_data[1];
            
        } else {
            
            $configrez['width'] = $upload_img_data[0];
            $configrez['height'] = $upload_img_data[0];
            $configrez['x_axis'] = 0;
            $configrez['y_axis'] = ($upload_img_data[1] - $upload_img_data[0]) / 2;
            $water_mark = $upload_img_data[0];
            
        }
        $this->image_lib->initialize($configrez);
        $this->image_lib->crop();

        $configrez2['image_library'] = 'gd2';
        $configrez2['source_image'] = $path;
        $configrez2['create_thumb'] = false;
        $configrez2['maintain_ratio'] = false;
        $configrez2['width'] = "800";
        $configrez2['height'] = "800";
        $this->image_lib->initialize($configrez2);
        $this->image_lib->resize();

        $configwm['source_image'] = $path;
        $configwm['wm_overlay_path'] = './images/watermark.png';
        $configwm['wm_type'] = 'overlay';
        $configwm['wm_opacity'] = '100';
        $configwm['wm_vrt_alignment'] = 'middle';
        $configwm['wm_hor_alignment'] = 'center';
        $this->image_lib->initialize($configwm);
        $this->image_lib->watermark();
        
        echo 'successfuly uploaded';
                
    }
                    

Leave a Comment:
mcsfashan@gmail.com2018-10-12 04:12:43
Good Job
Saranga Kumari2018-10-12 05:21:31
nice
Ajay2018-11-17 09:25:01
great
safiul2019-04-29 07:46:16
When try to upload same file which already uploaded show an unexpected result
Manish2019-11-15 11:53:04
make a video uploading image with manipulation in codeigniter
manish2019-11-16 06:45:09
how to crop upload image database
Manish2019-11-15 11:51:34
how to upload image in database

Search
Latest Articles