创建多个图像的缩略图?

时间:2015-10-02 00:32:14

标签: image codeigniter

我正在上传最多5张图片,我想创建这5张图片的缩略图我成功上传并在数据库中保存图片名称但无法制作缩略图。

//控制器

        $files = $_FILES;
        $cpt = count($_FILES['uploadfile']['name']);
        for($i=0; $i<$cpt; $i++)
                {
                $_FILES['uploadfile']['name']= $files['uploadfile']['name'][$i];
                $_FILES['uploadfile']['type']= $files['uploadfile']['type'][$i];
                $_FILES['uploadfile']['tmp_name']= $files['uploadfile']['tmp_name'][$i];
                $_FILES['uploadfile']['error']= $files['uploadfile']['error'][$i];
                $_FILES['uploadfile']['size']= $files['uploadfile']['size'][$i];
                $this->upload->initialize($this->set_upload_options());
                $this->upload->do_upload('uploadfile');
                $upload_data = $this->upload->data();
                $name_array[] = $upload_data['file_name']; //success till here now inserting in database and creating thumbnails
                $fileName = $upload_data['file_name'];
                $images[] = $fileName;
                }
              $fileName = $images;
              var_dump($images);
              @$form['picture1']=$images[0];
              @$form['picture2']=$images[1];
              @$form['picture3']=$images[2];
              @$form['picture4']=$images[3];
              @$form['picture5']=$images[4];

//

private function set_upload_options()
  { 
  // upload an image options
         $config = array();
         $config['upload_path'] = LARGEPATH; //give the path to upload the image in folder
         $config['remove_spaces']=TRUE;
         $config['encrypt_name'] = TRUE; // for encrypting the name
         $config['allowed_types'] = 'gif|jpg|png';
         $config['max_size'] = '78000';
         $config['overwrite'] = FALSE;
         return $config;
  }

现在我想要同时在文件夹中上传5张图片,同时创建这5张图片的缩略图。

2 个答案:

答案 0 :(得分:1)

您可以使用CodeIgniter的Image Manipulation Class创建缩略图:查看CodeIgniter 3 manual

答案 1 :(得分:0)

我找到了我的解决方案:

private function _makeThumb($source, $filename, $num = 5)
{
    $config2['image_library']  = 'gd2';
    $config2['source_image']   = $source;
    $config2['new_image']      = THUMBPATH.$filename;
    $config2['create_thumb']   = TRUE;
    $config2['maintain_ratio'] = TRUE;
    $config2['width']          = 75;
    $config2['height']         = 50;
    return $config2;


}

然后我可以在循环中调用此函数来创建一个thumnails