多个文件上传 - 如何为所有上传的文件集成缩略图功能?

时间:2015-06-11 07:42:18

标签: php file codeigniter file-upload image-uploading

这些是我的多个上传文件的代码..

请您帮我处理交换代码还有缩略图集成

public function addimage($room_id)
{

    $name_array = array();
    $count = count($_FILES['userfile']['size']);
    foreach($_FILES as $key=>$value)
    for($s=0; $s<=$count-1; $s++) {
        $_FILES['userfile']['name']=$value['name'][$s];
        $_FILES['userfile']['type']    = $value['type'][$s];
        $_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
        $_FILES['userfile']['error']       = $value['error'][$s];
        $_FILES['userfile']['size']    = $value['size'][$s];  
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        //$config['encrypt_name'] = uniqid(date(1));
        // $config['max_size']  = '100';
        // $config['max_width']  = '1024';
        // $config['max_height']  = '768';
        $this->load->library('upload', $config);
        $this->upload->do_upload();
        $data = $this->upload->data();
        $name_array[] = $data['file_name'];
    } // end foreach
    $names= array($name_array);
    print_r($names);


    exit();

3 个答案:

答案 0 :(得分:0)

怎么样?

public function addimage($room_id)
{

    $this->load->library("image_lib");
    $name_array = array();
    $count = count($_FILES['userfile']['size']);
    foreach($_FILES as $key=>$value)
    for($s=0; $s<$count; $s++) {
        $_FILES['userfile']['name']=$value['name'][$s];
        $_FILES['userfile']['type']    = $value['type'][$s];
        $_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
        $_FILES['userfile']['error']       = $value['error'][$s];
        $_FILES['userfile']['size']    = $value['size'][$s];  
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        //$config['encrypt_name'] = uniqid(date(1));
        // $config['max_size']  = '100';
        // $config['max_width']  = '1024';
        // $config['max_height']  = '768';
        $this->load->library('upload', $config);
        $this->upload->do_upload();
        $data = $this->upload->data();
        $name_array[] = $data['file_name'];

        //thumbnail creation
        $config['image_library'] = 'gd2';
        $config['source_image'] = $data['file_name'];
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width']         = 200;
        $config['height']       = 200;
        $this->image_lib->initialize($config);
        $this->image_lib->resize();



    } // end foreach
    $names= array($name_array);
    print_r($names);


    exit();
}

答案 1 :(得分:0)

您应该使用以下命令重新初始化图像库:

$this->load->library('image_lib');
$image_config = array(
'source_image'      => $image_data['full_path'],
'new_image'         => $this->thumbs_path,
'maintain_ratio'    => true,
'width'             => 36,
'height'            => 36
);

$this->image_lib->clear();
$this->image_lib->initialize($image_config);  
$this->image_lib->resize();

答案 2 :(得分:0)

当我理解你的问题时,我会推荐你​​阅读有关codeigniter的图像处理库。

http://www.codeigniter.com/user_guide/libraries/image_lib.html

创建缩略图。成功上传后,您可以使用返回的数据创建缩略图。