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 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题