codeigniterimage-uploadingcodeigniter-3watermarkimagelibrary

Codeigniter Image watermark using image library


I want apply watermark on multiple images after uploading. Right now the images are uploading but all are without watermark and also I want to reduce the quality of uploading image.

public function do_upload()
{       
        $this->load->library('upload');

        $name_array = array();
        $files = $_FILES;
        $cpt = count($_FILES['userfile1']['name']);
        for($i=0; $i<=$cpt-1; $i++)
        {           
            $_FILES['userfile']['name']= $files['userfile1']['name'][$i];
            $_FILES['userfile']['type']= $files['userfile1']['type'][$i];
            $_FILES['userfile']['tmp_name']= $files['userfile1']['tmp_name'][$i];
            $_FILES['userfile']['error']= $files['userfile1']['error'][$i];
            $_FILES['userfile']['size']= $files['userfile1']['size'][$i];    

            $this->upload->initialize($this->set_upload_options());
            if($data = $this->upload->do_upload()){
                $config['source_image'] = $files['userfile1']['tmp_name'][$i]; //get original image
                $config['wm_type'] = 'overlay';
                $config['wm_overlay_path'] = './portfolio_img/ninja.png';
                $config['quality'] = 50;
                $config['wm_vrt_alignment'] = 'bottom';
                $config['wm_hor_alignment'] = 'right';
                $this->load->library('image_lib', $config);
                $this->image_lib->watermark(); 
            }

                $name_array[] = $this->upload->data('file_name');
        }

        return $name_array;
}  

private function set_upload_options()
{   
        //upload an image options
        $config = array();
        $config['upload_path']   = './portfolio_img/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']      = '0';
        $config['overwrite']     = FALSE;

        return $config;
}

Solution

  • Watermark method need image path you are passing tem_path hope this will work

    try this

    $config['source_image'] = './portfolio_img/' . $files['userfile1']['name'][$i]
    

    it is working for me

    UDATE

    please load library before the loop and change source_image name each time.

    $config['source_image'] = '';
    $config['wm_type'] = 'overlay';
    $config['wm_overlay_path'] = './portfolio_img/ninja.png';
    $config['quality'] = 50;
    $config['wm_vrt_alignment'] = 'bottom';
    $config['wm_hor_alignment'] = 'right';
    $this->load->library('image_lib', $config);
    
    for($i=0; $i<=$cpt-1; $i++)
    {           
        $_FILES['userfile']['name']= $files['userfile1']['name'][$i];
        $_FILES['userfile']['type']= $files['userfile1']['type'][$i];
        $_FILES['userfile']['tmp_name']= $files['userfile1']['tmp_name'][$i];
        $_FILES['userfile']['error']= $files['userfile1']['error'][$i];
        $_FILES['userfile']['size']= $files['userfile1']['size'][$i];    
    
        $this->upload->initialize($this->set_upload_options());
        if($data = $this->upload->do_upload()){
            $config['source_image'] = './portfolio_img/' . $files['userfile1']['name'][$i]
            $this->image_lib->initialize($config);
            $this->image_lib->watermark(); 
        }
    
        $name_array[] = $this->upload->data('file_name');
    }