2011-10-14 51 views
0

我正在一個項目的時刻,我需要重新取樣上傳的圖像3次,我的問題是,如果我只上傳1圖像,圖像只重新採樣一次,我不知道到哪裏它僅重採樣一次,下面是我的代碼,循環問題與重新採樣圖像3次

if(is_array($this->input->post()) && $this->input->post('uploader_count') != "") 
    { 
     for($i=0; $i < (int)$this->input->post('uploader_count'); $i++) 
     { 
      //set up the first image manipulation, this goes from source image to a 209 wide image 
      $config['image_library'] = 'gd2'; 
      $config['source_image'] = './media/uploads/headshots/'.$_POST['uploader_'.$i.'_name']; 
      $config['create_thumb'] = FALSE; 
      $config['maintain_ratio'] = FALSE; 
      $config['new_image'] = './media/uploads/headshots/width_209_'.$_POST['uploader_'.$i.'_name']; 
      $config['width'] = 209; 

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

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

      if(!$this->image_lib->resize()) 
      { 
       echo $this->image_lib->display_errors(); 
      } 
      else 
      { 
       $image = array(
        'url' => $config['new_image'], 
        'asset_type' => 'image', 
        'asset_size' => 'large', 
        'date_uploaded' => date("Y-m-d h:i:s"), 
        'candidates_candidate_id' => $this->session->userdata('candidates_candidate_id') 
       ); 

       $this->candidates_assets_model->insert($this->security->xss_clean($image)); 

       //this goes from a 209 image to a 104 
       $config2['image_library'] = 'gd2'; 
       $config2['source_image'] = './media/uploads/headshots/width_209_'.$_POST['uploader_'.$i.'_name']; 
       $config2['create_thumb'] = FALSE; 
       $config2['maintain_ratio'] = FALSE; 
       $config2['new_image'] = './media/uploads/headshots/width_104_'.$_POST['uploader_'.$i.'_name']; 
       $config2['width'] = 104; 

       $this->image_lib->clear(); 
       $this->image_lib->initialize($config2); 

       if(!$this->image_lib->resize()) 
       { 
        echo $this->image_lib->display_errors(); 
       } 
       else 
       { 
        $image = array(
         'url' => $config2['new_image'], 
         'asset_type' => 'image', 
         'asset_size' => 'medium', 
         'date_uploaded' => date("Y-m-d h:i:s"), 
         'candidates_candidate_id' => $this->session->userdata('candidates_candidate_id') 
        ); 

        $this->candidates_assets_model->insert($this->security->xss_clean($image)); 

        //this goes from a 104 image to 60 
        $config3['image_library'] = 'gd2'; 
        $config3['source_image'] = './media/uploads/headshots/width_104_'.$_POST['uploader_'.$i.'_name']; 
        $config3['create_thumb'] = FALSE; 
        $config3['maintain_ratio'] = FALSE; 
        $config3['new_image'] = './media/uploads/headshots/width_60_'.$_POST['uploader_'.$i.'_name']; 
        $config3['width'] = 60; 

        $this->image_lib->clear(); 
        $this->image_lib->initialize($config3); 

        if(!$this->image_lib->resize()) 
        { 
         echo $this->image_lib->display_errors(); 
        } 
        else 
        { 
         $image = array(
          'url' => $config3['new_image'], 
          'asset_type' => 'image', 
          'asset_size' => 'small', 
          'date_uploaded' => date("Y-m-d h:i:s"), 
          'candidates_candidate_id' => $this->session->userdata('candidates_candidate_id') 
         ); 

         $this->candidates_assets_model->insert($this->security->xss_clean($image)); 
        } 
       } 
      } 
     } 

回答

0

有這個代碼去,看看你是否仍然有問題:

if (is_array($this->input->post()) && $this->input->post('uploader_count') != "") { 

    // This (probably) only needs to be done once at the beginning 
    $this->load->library('image_lib'); 

    $baseImage = array (// These options are the same for every resample 
    'asset_type' => 'image', 
    // Set this once at the beginning, so you get the actual upload time, not the time the resample completed 
    'date_uploaded' => date("Y-m-d h:i:s"), 
    'candidates_candidate_id' => $this->session->userdata('candidates_candidate_id') 
); 

    for ($i = 0; $i < (int) $this->input->post('uploader_count'); $i++) { // Loop uploaded images 

    $baseConfig = array (// These options are the same for every resample 
     'image_library' => 'gd2', 
     // You can use the original upload as the source for each resample - this will result in better quality 
     'source_image' => './media/uploads/headshots/'.$_POST["uploader_{$i}_name"], 
     'create_thumb' => FALSE, 
     'maintain_ratio' => FALSE 
    ); 

    // Since you are doing the same thing over and over, you can tidy it up with a foreach() 
    foreach (array (209=>'large', 104=>'medium', 60=>'small') as $width => $sizeName) { 

     // Fetch the base config and set options for this iteration 
     $config = $baseConfig; 
     $config['new_image'] = "./media/uploads/headshots/width_{$width}_".$_POST["uploader_{$i}_name"]; 
     $config['width'] = $width; 

     // Set up the image manipulation 
     $this->image_lib->clear(); 
     $this->image_lib->initialize($config); 

     if (!$this->image_lib->resize()) { // If resize fails, move onto next uploaded image 
     echo $this->image_lib->display_errors(); 
     continue 2; 
     } 

     // Fetch the base image and set options for this iteration 
     $image = $baseImage; 
     $image['url'] = $config['new_image']; 
     $image['asset_size'] = $sizeName; 

     // Do the insert for this image data 
     $this->candidates_assets_model->insert($this->security->xss_clean($image)); 

    } // End foreach 

    } // End for 

} // End if