普通网友 2013-04-21 11:33
浏览 82

Codeigniter - 上传图像,生成缩略图,保存数据库中的两个目录

This is my model:

I am getting the following error

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

My question: Why can't I use data() to get the ['full_path'] from the saved thumbnail (like I did for upload)?

Is there a better way to do this? Thanks!

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

2条回答 默认 最新

  • drvonr6573 2013-04-21 16:44
    关注

    You can't get the ['full_path'] from data() on the image lib because $this->image_lib is not an instance of the upload library and simply doesn't have that method (as the error message states).

    With your config now, the image_lib will create the new resized image under the same filename that your original image and save it under the directory you specify in confing's new_image. The resulting full path will be saved into the $full_dst_path property of the image_lib and there's $dest_folder and $dest_image for the folder and the filename only too.

    So to use these, just drop the line:

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

    And when saving just write:

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

    Handling the errors that the Upload or Image_lib library could return would be a good idea too.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c