Create multiple thumbnails and save images to different directories in Codeigniter

I want different thumbs in different folders like original_pic and thumb pointers, only the original_pic image, but the thumb folder shows empty.

enter image description here

Below is the code I tried:

Here is my controller code:

public function gallerySave()
{
    //Start upload file
    if(!empty($_FILES['gallery_image']['name'])){ //if($_FILES['image']['error'] == 0){

        //Specification upload
        $config = array();
        $config['upload_path'] = 'uploads/gallery/photo';
        $config['allowed_types'] = 'jpg|jpeg|png|gif';
        //$config['file_name'] = date("Y-m-d-H-i-s")."_".str_replace(' ', '-', $_FILES['gallery_image']['name']);

        //Renaming file name by id
        $temp['gallery_image'] = 'tempdata';
        $tempDataSaveId = $this->MyModel->save('tbl_photogallery',$temp);

        if($tempDataSaveId){
            $filename = date("Y-m-d-H-i-s")."_".str_replace(' ', '-', $_FILES['gallery_image']['name']);
            $extension = pathinfo($filename, PATHINFO_EXTENSION);
            $config['file_name'] = $tempDataSaveId.'.'.$extension;
        }
        //End Renaming file name by id

        //Load upload library and initialize configuration
        $this->load->library('upload',$config);
        $this->upload->initialize($config);

        if($this->upload->do_upload('gallery_image')){
            $finfo=$this->upload->data();
            $this->create_thumbs($finfo['file_name']);
            $gallery_image = $finfo['raw_name'].$finfo['file_ext'];
        }else{
            $gallery_image = '';
        }

    }
    //End upload file

    if (!empty($_POST['pic_title']) or !empty($_POST['gallery_image'])){
        $data = $this->input->post();

        if (!empty($gallery_image)){
            $data['gallery_image'] = $gallery_image;
        }

        //update or save data to gallery
        if (!empty($tempDataSaveId)){
            $response = $this->MyModel->update('tbl_photogallery',$tempDataSaveId, $data);
            if ($response)
                $result = $this->MyModel->FindById('tbl_photogallery', $tempDataSaveId);
        }else{
            $response = $this->MyModel->save('tbl_photogallery',$data);
            if ($response)
                $result = $this->MyModel->FindById('tbl_photogallery',$response);
        }

        if ($response) {
            //unlink raw file
            unlink('uploads/gallery/photo/'.$result->gallery_image);
            $sdata['success_alert'] = "Saved successfully";
        }else{
            $sdata['failure_alert'] = "Not Saved successfully";
        }
        $this->session->set_userdata($sdata);

        redirect('back/galleryCreate');
    }else{
        $sdata['failure_alert'] = "Try again";
        $this->session->set_userdata($sdata);
        redirect('back/galleryCreate');
    }

}
/*
 * Thumbnail creator
 */
function create_thumbs($filename)
{
    $config['image_library']    = "gd2";
    $config['source_image']     = "uploads/gallery/photo/".$filename;

    $config['new_image']        = "uploads/gallery/photo/original_pic/".$filename;
    $config['create_thumb']     = False;
    $config['maintain_ratio']   = TRUE;
    $config['width']            = "800";
    $config['height']           = "530";
    $this->load->library('image_lib', $config);
    if(!$this->image_lib->resize())
    {
        echo $this->image_lib->display_errors();
    }

    $config['new_image']        = "uploads/gallery/photo/thumb/". $filename;
    $config['width']            = "120";
    $config['height']           = "90";
    $this->load->library('image_lib', $config);
    if(!$this->image_lib->resize())
    {
        echo $this->image_lib->display_errors();
    }
}
//End Thumbnail creation

      

+3


source to share


1 answer


use Codeigniter Image Manipulation Class . You can find the documentation here . Also check the code below to create thumbnails and save in different directories.

$filename = $this->input->post('fileToUpload');
$source_path = FCPATH . '/uploads/source/tmp/' . $filename;
$target_path = FCPATH. '/uploads/thumb/';
$config_manip = array(
    'image_library' => 'gd2',
    'source_image' => $source_path,
    'new_image' => $target_path,
    'maintain_ratio' => TRUE,
    'create_thumb' => TRUE,
    'thumb_marker' => '_thumb',
    'width' => 150,
    'height' => 150
);
$this->load->library('image_lib', $config_manip);
if (!$this->image_lib->resize()) {
    echo $this->image_lib->display_errors();
}
// clear //
$this->image_lib->clear();

      



This will help you.

0


source







All Articles