【问题标题】:How to fix width and height of image when upload multiple images using codeigniter?使用codeigniter上传多张图片时如何修复图片的宽度和高度?
【发布时间】:2019-01-29 09:23:28
【问题描述】:

控制器:

public function products()
{
    date_default_timezone_set("Asia/Calcutta");

    $dataInfo = array();
    $files = $_FILES;
    $cpt = count($_FILES['product_image']['name']);
    for($i=0; $i<$cpt; $i++)
    {           
        $_FILES['product_image']['name']= $files['product_image']['name'][$i];
        $_FILES['product_image']['type']= $files['product_image']['type'][$i];
        $_FILES['product_image']['tmp_name']= $files['product_image']['tmp_name'][$i];
        $_FILES['product_image']['error']= $files['product_image']['error'][$i];
        $_FILES['product_image']['size']= $files['product_image']['size'][$i];
        $this->upload->initialize($this->set_upload_options());
                $this->upload->do_upload('product_image');
                $upload_data = $this->upload->data();
                $name_array[] = $upload_data['file_name'];
                $fileName = $upload_data['file_name'];
                $images[] = $fileName;
    }

    $fileName = $images;
    $data = array(
            'product_image' => implode(",",$fileName)
        );
    $sql = $this->db->insert('add_product',$data);
    if($sql == true)
    {
        echo '<p style="color:green;">New Product Added</p>';
    }
    else
    {
        echo '<p style="color:red;">Unable to Proceed!</p>';
    }
}

private function set_upload_options()
{   
    $config = array();
    $config['upload_path'] = FCPATH.'resource/product/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['max_size']      = '1024';
    $config['overwrite']     = FALSE;
    return $config;
}

在这段代码中,我通过 jquery ajax 上传了多个图像,它们完美地工作。现在,我希望当我上传多张图片时,我还修复了所有图片的widht and height。那么,我该怎么做呢?请帮帮我。

谢谢

【问题讨论】:

    标签: jquery codeigniter upload


    【解决方案1】:

    使用set_upload_options()中的配置

    $config['upload_path']          = FCPATH.'resource/product/';
    $config['allowed_types']        = 'gif|jpg|png|jpeg';
    $config['max_size']             = 100;
    $config['max_width']            = 1024; //Change according to requirements
    $config['max_height']           = 768;  //Change according to requirements
    

    另外,在$this-&gt;upload-&gt;initialize($this-&gt;set_upload_options()); 之后使用$this-&gt;load-&gt;library('upload');

    更多关于文件上传类 read this

    【讨论】:

      【解决方案2】:

      images[] = $fileName;之后添加以下内容

      $this->_resize_image_large($upload_data['file_name'], your_width, your_height, 'your_folder', 'your_folder/large/');
      

      这里是函数

      private function _resize_image_large($file_path,$width,$height,$old_path,$new_path){
          $this->load->library('image_lib');
          $configlarge['image_library'] = 'gd2';
          $configlarge['source_image'] = $old_path.$file_path;
          $configlarge['new_image'] = $new_path.$file_path;
          $configlarge['maintain_ratio'] = FALSE;
          $configlarge['width'] = $width;
          $configlarge['height'] = $height;
          $this->image_lib->clear();
          $this->image_lib->initialize($configlarge);
          $this->image_lib->resize();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-11-18
        • 2018-11-07
        • 1970-01-01
        • 2015-02-06
        • 1970-01-01
        • 2017-04-08
        • 2011-12-10
        相关资源
        最近更新 更多