调整多个图像区域的大小

resizing multiple fields of image

我有一个表单,其中包含一些图像文件字段,将所有这些值提交给我的控制器, 收到所有字段后,我只能调整图像文件中一个字段的大小,其余图像字段不会调整大小。

这是我的看法

<input type="file" name="image" size="20" />
<input type="file" name="image2" size="20" />
<input type="submit" value="upload" /> 

这是我的控制器

public function resizeImage($filename){
  $source_path = './upload/' . $filename;
  $target_path = './upload/';
  $config_manip = array(
    'image_library' => 'gd2',
    'source_image' => $source_path,
    'new_image' => $target_path,
    'maintain_ratio' => TRUE,
    'width' => 500,
  );
  $this->load->library('image_lib', $config_manip);
  if (!$this->image_lib->resize()) {
     echo $this->image_lib->display_errors();
  }
  $this->image_lib->clear();
}

public function uploadImage() { 
  $config['upload_path']   = './upload/'; 
  $config['allowed_types'] = 'gif|jpg|png'; 
  $config['max_size']      = 1024;
  $this->load->library('upload', $config);
  if ($this->upload->do_upload('image')) {
      $error = array('error' => $this->upload->display_errors()); 
      $uploadedImage = $this->upload->data();
      $this->resizeImage($uploadedImage['file_name']);
      print_r('Image Uploaded Successfully.');
      /*exit;*/
  }
  if ($this->upload->do_upload('image2')) {
      $error = array('error' => $this->upload->display_errors()); 
      $uploadedImage = $this->upload->data();
      $this->resizeImage($uploadedImage['file_name']);
      print_r('Image Uploaded Successfully.');
      /*exit;*/
   } 
}

为了调整图像大小,我做了一个函数,我传递图像细节和图像可以调整它们的大小。但它只调整了一次,第二次以正常大小上传图片。

当您使用 codeigniter 上传库时,要更改配置,您必须使用 initialize

public function resizeImage($filename){
  $source_path = './upload/' . $filename;
  $target_path = './upload/';
  $config_manip = array(
    'image_library' => 'gd2',
    'source_image' => $source_path,
    'new_image' => $target_path,
    'maintain_ratio' => TRUE,
    'width' => 500,
  );
  //change load library to initialize
  $this->image_lib->initialize($config_manip); 
  if (!$this->image_lib->resize()) {
     echo $this->image_lib->display_errors();
  }
  $this->image_lib->clear();
}

$this->load->library('upload', $config);
$this->load->library('image_lib');//load library outside your resizeImage function
  if ($this->upload->do_upload('image')) {
      $error = array('error' => $this->upload->display_errors()); 
      $uploadedImage = $this->upload->data();
      $this->resizeImage($uploadedImage['file_name']);
      print_r('Image Uploaded Successfully.');
      /*exit;*/
  }
  if ($this->upload->do_upload('image2')) {
      $error = array('error' => $this->upload->display_errors()); 
      $uploadedImage = $this->upload->data();
      $this->resizeImage($uploadedImage['file_name']);
      print_r('Image Uploaded Successfully.');
      /*exit;*/
   }