2010-01-15 195 views
1

我做用笨的Multiple_upload庫中的多個上傳充分循環,並形成了一些我的循環,它似乎在這裏第一次迭代中產生縮略圖只火是我的代碼循環不actioning在第一次迭代

function saveContentImages() { 
    $this->load->model('categoryModel'); 
    if($query = $this->categoryModel->getCategoryByContentId($this->input->post('contentTitle'))){ 
     foreach($query as $k => $v) { 
      $categoryTitle = strtolower($v['categoryTitle']); 
     } 
    } 
    // we now need to set up the configuration that the upload 
    // library expects to see. 
    $config['upload_path'] = 'media/uploads/'.$categoryTitle; 
    $config['allowed_types'] = 'gif|jpg|jpeg|png'; 
    $config['max_size'] = '1000'; 
    $sonfig['max_width'] = '1024'; 
    $config['max_height'] = '768'; 
    if(!file_exists($config['upload_path'])) { 
     mkdir($config['upload_path'], 0777); 
    } 
    // load in both the libaries that the image upload we will 
    // we import codeigniters own upload library and also a library 
    // from the community that allows for multiple uploads with the help 
    // of jQuery 
    $this->load->library('upload', $config); 
    $this->load->library('Multi_upload'); 
    // we can now do the multiple upload 
    $files = $this->multi_upload->go_upload(); 
    echo $this->upload->display_errors(); 
    if(!$files) { 
     $this->load->model('categoryModel'); 
     if($query = $this->categoryModel->getCategoryByContentId($this->input->post('contentTitle'))){ 
      foreach($query as $k => $v) { 
       $categoryTitle = strtolower($v['categoryTitle']); 
      } 
     } 
    } else { 
     // we now need to do some GD library work so that the content can 
     // have thumbnail images 
      foreach ($files as $image) { 
      $gd['image_library'] = 'gd2'; 
      $gd['source_image'] = $image['file']; 
      $gd['create_thumb'] = TRUE; 
      $gd['maintain_ratio'] = TRUE; 
      $gd['width'] = 63; 
      $gd['height'] = 48; 

      $this->load->library('image_lib', $gd); 
      $resize = $this->image_lib->resize(); 
      echo $resize."<br />"; 
      // this condition gets run if the resize fails 
      echo $this->image_lib->display_errors(); 
      if(!$resize) { 
       echo $this->image_lib->display_errors(); 
      } 
     } 
     // loop through the $files array and save each image in the array 
     foreach($files as $image) { 
      $this->load->model('imageModel'); 
      $query = $this->imageModel->saveContentImages($this->input->post('contentTitle'), $image); 
     } 
     $data['formSubmitted'] = "Images"; 
     $this->load->view('admin/successPage', $data); 
    } 
} 

,這是$文件數組當我print_r

( [0] =>數組 ( [名稱] => orange.png [文件] => /用戶/西蒙/站點/ mysite的/ media/uploads/blog/orange.png [尺寸] => 3.07 [EXT] => .PNG [IMAGE_TYPE] =>爲png [高度] => 703 [寬度] => 1000 )

[1] => Array 
    (
     [name] => yellow.png 
     [file] => /Users/Simon/Sites/mysite/media/uploads/blog/yellow.png 
     [size] => 3.06 
     [ext] => .png 
     [image_type] => png 
     [height] => 703 
     [width] => 1000 
    ) 

[2] => Array 
    (
     [name] => purple.png 
     [file] => /Users/Simon/Sites/mysite/media/uploads/blog/purple.png 
     [size] => 3.07 
     [ext] => .png 
     [image_type] => png 
     [height] => 703 
     [width] => 1000 
    ) 

是否有任何理由會這樣做,還是有我可以做的任何檢查?

+0

sico87,我不太確定問題是什麼......是文件沒有上傳或是僅爲第一個文件創建的縮略圖預覽? – 2010-01-16 00:35:52

回答

2

CodeIgniter類加載器不會加載一個類的多個實例,除非爲每個實例指定了不同的對象名稱。所以,由於image_lib構造函數沒有運行,所以配置選項沒有更新。您應該在進入循環之前加載該類,然後手動初始化並每次通過循環清除它。

 

$gd = array(); 
$this->load->library('image_lib'); 
foreach ($files as $image) { 

    $gd['image_library'] = 'gd2'; 
    $gd['source_image'] = $image['file']; 
    $gd['create_thumb'] = TRUE; 
    $gd['maintain_ratio'] = TRUE; 
    $gd['width'] = 63; 
    $gd['height'] = 48; 

    $this->image_lib->initialize($gd); 
    $resize = $this->image_lib->resize(); 

    $this->image_lib->clear(); 
}