多次加载Codeigniter库

时间:2014-09-22 01:16:41

标签: php codeigniter

好的,所以我在同一个控制器功能中对同一个库有两个独立的配置。第一个运行得很好,但第二个运行不正常。我如何使它成为第一个codeigniter unsets并使用第二个配置。

// Retrieve the data from the upload
        $data = $this->upload->data();

        //Re-size the large image and re-save it
        $config['image_library'] = 'gd2';
        $config['source_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/'.$file_name.''.$data['file_ext'];
        $config['new_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/';
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = TRUE;
        $config['master_dim'] = 'auto';
        $config['width']    = 600;
        $config['height']   = 516;

        $this->load->library('image_lib', $config);

        if ( ! $this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }
        else
        {
            $this->image_lib->crop();
            echo 'Image Resized!';
        }

        //Create a thumbnail for the image
        $config['image_library'] = 'gd2';
        $config['source_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/'.$file_name.''.$data['file_ext'];
        $config['new_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/thumbnails/';
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width']    = 120;
        $config['height']   = 120;

        $this->load->library('image_lib', $config); 

        if ( ! $this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }
        else
        {
            echo 'Image Thumbnail Created!';
        }

因此,它会重新调整图像大小,然后应该缩略图像。

1 个答案:

答案 0 :(得分:4)

您可以使用library提供的初始化方法,因此您每次都可以调用它而不是加载库:

$this->load->library('image_lib');

$config['image_library'] = 'gd2';
        $config['source_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/'.$file_name.''.$data['file_ext'];
        $config['new_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/';
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = TRUE;
        $config['master_dim'] = 'auto';
        $config['width']    = 600;
        $config['height']   = 516;

$this->image_lib->initialize($config); 

...

$config['image_library'] = 'gd2';
        $config['source_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/'.$file_name.''.$data['file_ext'];
        $config['new_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/thumbnails/';
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width']    = 120;
        $config['height']   = 120;

 $this->image_lib->initialize($config);