2015-09-30 39 views
2

我有这样的代码在笨文件上载:Codeigniter - 如何上传带缩略图的原始图像?

if(!empty($_FILES['userfile'])){ 
    $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'] = './public/images/campaign-images/'; 
      $config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG'; 
      $config['max_size'] = '10000'; 
      //$config['max_width'] = '1024'; 
      //$config['max_height'] = '768'; 
      $CI->load->library('upload', $config); 
      $CI->upload->do_upload(); 
      $data = $CI->upload->data(); 
      $name_array[] = $data['file_name']; 
     } 
    return $name_array; 
} 

此代码工作完美单原图上传,但如何上传图片缩略图像(350×250),具有不同的文件夹中调整大小。

任何想法如何处理codeigniter库?

感谢

回答

3

首先存储原始图像和之后的缩略图将上传 你只需要包括GD2库生成缩略图

  if(!empty($_FILES['userfile'])){ 
       $name_array = array(); 
       $count = count($_FILES['userfile']['size']); 
       foreach($_FILES as $key => $value) 
        for ($s=0; $s<=$count-1; $s++) 
        { 
         //Original Image Upload - Start 
         $_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'] = './public/images/campaign-images/'; 
         $config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG'; 
         $config['max_size'] = '10000'; 
         //$config['max_width'] = '1024'; 
         //$config['max_height'] = '768'; 
         $CI->load->library('upload', $config); 
         $CI->upload->do_upload(); 
         $data = $CI->upload->data(); 
         //Original Image Upload - End 

         //Thumbnail Image Upload - Start 
         $config['image_library'] = 'gd2'; 
         $config['source_image'] = './public/images/campaign-images/'. $value['name'][$s]; 
         $config['new_image'] = './public/images/campaign-images/thumbs/'.$value['name'][$s]; 
         $config['width'] = 350; 
         $config['height'] = 250; 

         //load resize library 
         $this->load->library('image_lib', $config); 
         $this->image_lib->resize(); 
         //Thumbnail Image Upload - End 

         $name_array[] = $data['file_name']; 
        } 
       return $name_array; 
      } 
+0

谢谢大家对我的支持。我已添加您的代码,但无法在拇指文件夹中看到上传的文件。 –

+1

只需修改源图像路径如下 $ config ['source_image'] = $ value ['name'] [$ s]; –

+0

在哪里添加? –

0
if(!empty($_FILES['userfile'])){ 
      $name_array = array(); 
      $count = count($_FILES['userfile']['size']); 
      foreach($_FILES as $key => $value) 
       for ($s=0; $s<=$count-1; $s++) 
       { 
        //Original Image Upload - Start 
        $_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'] = 'PATH_TO_UPLOAD_IMAGE'; 
        $config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG'; 
        $config['max_size'] = '10000'; 
        $CI->load->library('upload', $config); 
        $CI->upload->do_upload(); 
        $data = $CI->upload->data(); 
        //Original Size of Image Upload - End 

        //Thumbnail Size of Image Upload - Start 
        $config['image_library'] = 'gd2'; 
        $config['source_image'] = 'PATH_TO_IMAGE_UPLOAD'. $value['name'][$s]; 
        $config['new_image'] = 'PATH_TO_UPLOAD_THUMB_IMAGE'.$value['name'][$s]; 
        $config['width'] = 350; 
        $config['height'] = 250; 

        //CI load resize library 
        $this->load->library('image_lib', $config); 
        $this->image_lib->resize(); 
        //Thumbnail SIZED Image Upload - End 

        $name_array[] = $data['file_name']; 
       } 
      return $name_array; 
     } 
相关问题