Codeigniter将单个图像转换为多种尺寸,即350 * 250、50 * 50和原始尺寸

时间:2018-07-17 05:57:28

标签: codeigniter

我是CodeIgniter的新手。这就是我想做的:

  1. 将产品图片的尺寸调整为350 * 250、50 * 50和原始图片,同时从管理员上载添加产品中的图片。

  2. 所有这三个图像都位于三个不同的文件夹中。

    这是我的代码-

    public function products()
    {
        $config=array();
        $config['upload_path']="./uploads/";
        $config['allowed_types']="jpg|jpeg|gif|png";
        $this->load->library('upload',$config);
        $this->upload->initialize($config);
        if ( ! $this->upload->do_upload('userfile')) 
        {
            $error = array('error' => $this->upload->display_errors());                
        }
        else 
        {              
            $file_data = $this->upload->data();
            $data1['img']=base_url().'images/'.$file_data['file_name'];
            $data_image = array(
                    'agenda_id' => $this->input->post('agenda_id'),
                    'file'      => $file_data['file_name']
            );
    
          $config['image_library']='gd2';
          $config['source_image']='./uploads/'.$file_data["file_name"];
          $config['create_thumb']=FALSE;
          $config['maintain_ratio']=FALSE;
          $config['quality']='60%';                   
          $config['width']=350;
          $config['height']=250;
          $config['new_image']='./uploads350/'.$file_data["file_name"];
          $this->upload->initialize($config);
          $this->load->library('image_lib',$config);
          $this->image_lib->resize();
         }  
         $this->load->model("Admin_model");
         $data= array(
           "Type" =>$this->input->post("Type"),
           "Brand" =>$this->input->post("Brand"),
           "Product_Name" =>$this->input->post("Product_Name"),
           "Price" =>$this->input->post("Price"),
           "Image"=>$data_image['file']
    
        );
        $this->Admin_model->admin($data);
    
        $this->session->set_flashdata('message', 'Product added successfully');
    
        redirect('Admin/product_list');    
    }
    

1 个答案:

答案 0 :(得分:0)

这应该可以正常工作(未经测试)。只要确保文件夹存在。另外,如果我是你,我可能会将所有内容都保留在同一文件夹中,而只需在some_image_50.jpg之类的后缀后面加上

public function products() {
        $config = array();
        $config['upload_path'] = "./uploads/";
        $config['allowed_types'] = "jpg|jpeg|gif|png";
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        if (!$this->upload->do_upload('userfile')) {
            $error = array('error' => $this->upload->display_errors()); // do something with this...
            // e.g. show_error($this->upload->display_errors());
        } else {
            $file_data = $this->upload->data();
            $data1['img'] = base_url() . 'images/' . $file_data['file_name'];
            $data_image = array(
                'agenda_id' => $this->input->post('agenda_id'),
                'file' => $file_data['file_name']
            );
            //$config['image_library'] = 'gd2'; (default; not needed)
            $this->load->library('image_lib');
            $cfg['source_image'] = './uploads/' . $file_data["file_name"];
            $cfg['create_thumb'] = FALSE;
            $cfg['maintain_ratio'] = FALSE;
            $cfg['quality'] = '60%';
            $cfg['width'] = 350;
            $cfg['height'] = 250;
            $cfg['new_image'] = './uploads350/' . $file_data["file_name"];
            //$this->upload->initialize($config); why??
            $this->image_lib->initialize($cfg);
            $this->image_lib->resize();
            // the following vars will override certain vars above and keep the ones we haven't overridden
            $cfg['width'] = 50;
            $cfg['height'] = 50;
            $cfg['new_image'] = './uploads50/' . $file_data["file_name"]; // make sure folder exists
            $this->image_lib->initialize($cfg);
            $this->image_lib->resize();
        }
        $this->load->model("Admin_model");
        $data = array(
            "Type" => $this->input->post("Type"),
            "Brand" => $this->input->post("Brand"),
            "Product_Name" => $this->input->post("Product_Name"),
            "Price" => $this->input->post("Price"),
            "Image" => $data_image['file']
        );
        $this->Admin_model->admin($data);

        $this->session->set_flashdata('message', 'Product added successfully');

        redirect('Admin/product_list');
    }