0

这是我的模型:

我收到以下错误

 Fatal error: Call to undefined method CI_Image_lib::data() in C:\xampp\htdocs\adcc\application\models\media_model.php on line 58

我的问题:为什么我不能使用 data() 从保存的缩略图中获取 ['full_path'] (就像我为上传所做的那样)?

有一个更好的方法吗?谢谢!

public function set_media() {

        $config1 = array(
            'allowed_types' => 'jpg|jpeg|gif|png',
            'upload_path' => $this->gallery_path . '/images',
            'max_size' => 2048
        );

        $this->load->library('upload');
        $this->upload->initialize($config1);
        $this->upload->do_upload();

        $image_data = $this->upload->data();

        $config2 = array(
            'source_image' => $image_data['full_path'],
            'new_image' => $this->gallery_path . '/thumbs',
            'maintain_ratio' => true,
            'width' => 150,
            'height' => 100
        );

        $this->load->library('image_lib', $config2);
        $this->image_lib->resize();
        $image_data2 = $this->image_lib->data();

        $this->load->helper('url');

        $id = url_title($this->input->post('title'), 'dash', TRUE);

        $data = array(
            'id' => $id,
            'name' => $this->input->post('name'),
            'link' => $this->input->post('link'),
            'year' => $this->input->post('year'),
            'actors' => $this->input->post('actors'),
            'image' => $image_data['full_path'],
            'thumb' => $image_data2['full_path']
        );

        return $this->mongo_db->insert('media', $data);
    }
4

3 回答 3

1

您无法从图像库上的 data() 获取 ['full_path'] ,因为$this->image_lib它不是上传库的实例,并且根本没有该方法(如错误消息所述)。

现在使用您的配置,image_lib 将以与原始图像相同的文件名创建新的调整大小的图像,并将其保存在您在 confing 中指定的目录下new_image。生成的完整路径将保存到$full_dst_pathimage_lib 的属性中,并且$dest_folder文件$dest_image夹和文件名也只有和。

因此,要使用这些,只需删除以下行:

$image_data2 = $this->image_lib->data(); // delete this line

保存时只需写:

$data = array(
// ...
'thumb' => $this->image_lib->full_dst_path,
// ...
),

处理 Upload 或 Image_lib 库可能返回的错误也是一个好主意。

于 2013-04-21T16:44:28.633 回答
0

在调整大小之前 ,您还需要初始化image_lib配置数组的部分,这对我有用 https://stackoverflow.com/a/21187402/2897770

只需在加载image_lib $this->image_lib->initialize($config2);后添加它

于 2014-01-17T13:43:05.893 回答
0

对于文件名:

$source_image_name = $this->image_lib->source_image;
                    $extension = strrchr($source_image_name , '.');
                    $name = substr($source_image_name , 0, -strlen($extension));                $thumbnail['thumbnail_image_name']$name.$image['thumb_marker'].$extension;
于 2021-09-25T10:41:36.810 回答