使用Codeigniter上传多张图片时如何固定图片的宽度和高度?

时间:2019-01-29 09:23:29

标签: jquery codeigniter mysqli upload

控制器:

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。那么,我该怎么做?请帮助我。

谢谢

2 个答案:

答案 0 :(得分:0)

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->load->library('upload');之后使用$this->upload->initialize($this->set_upload_options());

有关文件上传课程的更多信息 read this

答案 1 :(得分:0)

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();
}