【问题标题】:resizing multiple fields of image调整多个图像字段的大小
【发布时间】:2020-07-29 03:48:50
【问题描述】:

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

这是我的看法

<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;*/
   } 
}

为了调整图像大小,我创建了一个函数,我在其中传递图像详细信息,并且图像可以调整其大小。但它只调整一次大小,第二次以正常大小上传图片。

【问题讨论】:

    标签: php html codeigniter


    【解决方案1】:

    当您使用 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;*/
       } 
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-28
      • 2014-02-27
      • 2013-08-22
      • 1970-01-01
      相关资源
      最近更新 更多