douhuang1852 2017-07-13 10:09
浏览 46
已采纳

在Codeigniter中创建多个缩略图并在不同目录中保存图像

I want different thumbs in different folders like original_pic and thumb directories image goes only original_pic but thumb directory shows empty.

enter image description here

Below code I have tried:

Here is the code of my controller:

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
  • 写回答

1条回答 默认 最新

  • dongniaocheng3773 2017-07-13 10:33
    关注

    use Codeigniter's Image Manipulation Class. You can find documentation here, Also please check below code to generate thumbnails and save into 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog