2012-10-10 55 views
0

我用CI上传多张图像,他们上传。我也尝试用下面的代码调整它们的大小,
,只调整第一个图像,其余的不调整大小。他们会按照当前的大小上传。 有什么问题?CI多重图像调整大小

任何帮助是真的,真的很感激。

function doupload() { 

    $path = array(); 
    $count = count($_FILES['userfile']['size']); 

    foreach($_FILES as $key=>$value){ 
     for($n=0; $n<=$count-1; $n++) { 
      $_FILES['userfile']['name']=$value['name'][$n]; 
      $_FILES['userfile']['type'] = $value['type'][$n]; 
      $_FILES['userfile']['tmp_name'] = $value['tmp_name'][$n]; 
      $_FILES['userfile']['error']  = $value['error'][$n]; 
      $_FILES['userfile']['size'] = $value['size'][$n]; 

       $config['upload_path'] = './images'; 
       $config['allowed_types'] = 'gif|jpg|png|jpeg'; 

      $this->load->library('upload', $config); 
      $this->upload->do_upload(); 
      $data = $this->upload->data(); 
      $path[] = $data['full_path']; //contains full path of every image 
     } 
    } 

    foreach($path as $p=>$ath){ 
     $config1 = array(
     'source_image'  => $ath, 
     'new_image'   => './images', 
     'maintain_ration' => true, 
     'overwrite'   => true, 
     'width'    => 600, 
     'height'   => 400 
     ); 

     $this->load->library('image_lib', $config1);    
     $this->image_lib->resize(); 
     $this->image_lib->clear(); 

    } 


} 

回答

2

第一负载image_lib您可以使用循环初始化,并在我的情况负荷LIB在构造通过新配置的每张图片

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

    foreach($path as $p=>$ath){ 
      $config1 = array(
      'source_image'  => $ath, 
      'new_image'   => './images', 
      'maintain_ration' => true, 
      'overwrite'   => true, 
      'width'    => 600, 
      'height'   => 400 
      ); 

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

     } 
0

CodeIgniter的Loader类负载库只有一次,所以在你的foreach您调整同一图像多次。将库加载移出循环并使用Image Manipulation库的initialize方法为每个调整大小设置配置。外循环

0

,并使用代码

$config['image_library'] = 'gd2'; 
$config['maintain_ratio'] = FALSE; 
$config['source_image'] = $config['upload_path'].$image_info['file_name']; 

$config['new_image'] = $config['upload_path']."thumb_".$image_info['file_name']; 
$config['width'] = 313; 
$config['height'] = 303; 
$this->image_lib->initialize($config); 
$this->image_lib->resize(); 
$this->image_lib->clear(); 

$config['new_image'] = $config['upload_path']."icon_".$image_info['file_name']; 
$config['width'] = 70; 
$config['height'] = 70; 
$this->image_lib->initialize($config); 
$this->image_lib->resize(); 
$this->image_lib->clear(); 

保持指数名字始终new_image $config['new_image']