上传多个图像并生成大尺寸

时间:2014-03-02 12:41:17

标签: image codeigniter image-uploading

我已经生成了一个代码,它会在codeigniter中上传多个图像,但是我无法使用codeigniter图像库生成大尺寸的上传图像到其他目录。

使用下面的代码,即使用户上传了多张图片,它也只会生成一张图片。

  function insert_prod(){   

    $foldername = $_POST['folder'];
    $path = './uploads/products/'.$foldername;

    mkdir($path, 0777, true);
    mkdir($path.'/zoomimages', 0777, true);

    $config = array();
    $config['upload_path'] = $path;
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']      = '0';
    $config['overwrite']     = FALSE;

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

    $files = $_FILES;
    $cpt = count($_FILES['userfile']['name']);
    for($i=0; $i<$cpt; $i++){

        $_FILES['userfile']['name']= $files['userfile']['name'][$i];
        $_FILES['userfile']['type']= $files['userfile']['type'][$i];
        $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
        $_FILES['userfile']['error']= $files['userfile']['error'][$i];
        $_FILES['userfile']['size']= $files['userfile']['size'][$i];    

        $config['file_name'] = 'img_'.$i;

        $this->upload->initialize($config);
        $this->upload->do_upload();

        $upload_data = array('upload_data' => $this->upload->data());
        $full_path = $upload_data['upload_data']['full_path'];
        $u_data = $upload_data['upload_data'];
        // var_dump( $this->upload->data());
        // var_dump($upload_data); 
        echo $cpt;
        $config1['image_library'] = 'gd2';
        $config1['source_image']    = $full_path;
        $config1['new_image'] =$path.'/zoomimages';
        $config1['create_thumb'] = FALSE;
        $config1['maintain_ratio'] = TRUE;
        $config1['width']    = 300;
        $config1['height']  = 300;

        $this->load->library('image_lib', $config1); 
        $this->image_lib->resize();
        $this->image_lib->clear();
    }
}

1 个答案:

答案 0 :(得分:0)

您正在循环中加载$this->load->library('image_lib', $config1);库,因此codeigniter看到该类已加载,因此它未设置您的$config1 again

$this->load->library('image_lib');移出你的循环

然后添加$this->image_lib->initialize($config1);

$this->load->library('image_lib', $config1);瞬间