Codeigniter错误您必须在首选项中指定源图像

时间:2018-10-19 20:09:11

标签: php codeigniter codeigniter-3

当我尝试从上传的图像创建缩略图时收到以下错误:

  • 您必须在首选项中指定源图像。
  • 图像的路径不正确。
  • 您的服务器不支持处理此类图像所需的GD功能。

但是,当我回显源图像时,它已上传并且文件存在,但找不到图像。

控制器的功能

public function addgallery(){
        $year = date('Y');
        $date = str_replace( ':', '', $year);

        // Fetch a user or set a new one
        $this->data['page_title'] = 'Add New Gallery';
        $this->data['gallery'] = $this->gallery_m->get_new();

        // Set up the form
        $rules = $this->gallery_m->rules_admin;
        $this->form_validation->set_rules($rules);

        if ($this->form_validation->run() == TRUE || !empty($_FILES['thumbnail']['name'])) {
            if ( !$this->upload->do_upload('thumbnail')){
                $error = array('error' => $this->upload->display_errors());
                $this->load->view('upload_form', $error);
            }else{
                //file is uploaded successfully
                //now get the file uploaded data 
                $imagedata = $this->upload->data();

                //get the uploaded file name
                $image['pic_file'] = base_url().'assets/uploads/thumbnail/'.$date.'/'.$imagedata['file_name'].'-150x150';
                $thumbnail['pic_file'] = $this->gallery_m->resizeimage($imagedata['file_name']);
                $data = array('name' => $this->input->post('name'), 'description' => $this->input->post('description'), 'thumbnail' => $image['pic_file'], 'datesubmitted' => date('Y-m-d'));

                //store pic data to the db
                $this->gallery_m->qsave($data);
                $this->session->set_flashdata( 'message', 'Gallery Added Successfully' );
            }
        }

        // Load the view
        $this->data['subview'] = 'admin/gallery/addgallery';
        $this->load->view( 'admin/body', $this->data );
    }

模型中的功能

public function resizeimage($filename){
        $year = date('Y');
        $date = str_replace( ':', '', $year);
        $image_sizes = array(
            'thumb' => array(150, 100),
            'medium' => array(300, 300),
            'large' => array(800, 600)
        );
        $source_path = base_url().'assets/uploads/original/'.$date.'/'.$filename;
        $target_path = base_url().'assets/uploads/thumbnail/'.$date.'/';
        foreach ($image_sizes as $resize) {
            $config['image_library'] = 'GD2';
            $config['source_image'] = $source_path;
            $config['new_image'] = $target_path;
            $config['create_thumb'] = TRUE;
            $config['maintain_ratio'] = TRUE;
            $config['quality'] = '100';
            $config['thumb_marker'] = $resize[0].'x'.$resize[1];
            $config['width']     = $resize[0];
            $config['height']   = $resize[1];
            $this->load->library('image_lib');
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            if (!$this->image_lib->resize()) {
                echo $this->image_lib->display_errors();
                echo 'Source: '.$source_path.'<br /><br />';
                echo 'Target: '.$target_path.'<br /><br />';
            }
        }
        $this->image_lib->clear();
   }

在大多数情况下,该功能会按预期运行。图像已上传,并且图像和缩略图路径已保存到数据库。但是缩略图不会生成。

我尝试了一些在这里找到的示例,但似乎没有一个可以解决问题。

0 个答案:

没有答案