在CodeIgniter中上传时如何进行图片大小调整功能

时间:2017-07-11 09:49:03

标签: php codeigniter codeigniter-3

我在CodeIgniter框架上创建了一个函数,因此用户可以将产品图片上传到产品页面。问题是我希望将照片调整到我想要的宽度和高度。

这是我的控制器中的上传功能:

public function upload(){

$this->load->library('upload');

if(!$this->upload->do_upload('userfile')){
    $error = array('error'=>$this->upload->display_errors());
    $this->load->view('product_form', $error);
}else{

    $config['image_library'] = 'gd2';
    $config['source_image'] = './upload/'.$file_data["file_name"];
    $config['create_thumb'] = TRUE;
    $config['maintain_ratio'] = TRUE;
    $config['width']         = 100;
    $config['height']       = 100;

    //load library
    $this->load->library('image_lib', $config);

    //resize image
    $this->image_lib->resize();


    $this->db->insert('products', array(
        'product_foto' => $file_data['file_name'],
        'product_naam'  => $this->input->post('product_naam'),
        'product_beschrijving' => $this->input->post('product_beschrijving'),
        'product_categorie'  => $this->input->post('product_categorie'),
        'ophaal_plaats'  => $this->input->post('ophaal_plaats'), 
        'date_created' => date('Y-m-d'),
        'date_updated' => date('Y-m-d')
        ));
    $data['img'] = base_url().'/upload/'.$file_data['file_name'];
    header('location:https://kadokado-ferran10.c9users.io/Product/');
}

我的观点:

<?php echo form_open_multipart('Product/upload'); ?>


        <table class="aanbieding-cadeau">

         <tr>
          <td><?php echo form_input(array('id'=>'product_naam', 'name'=>'product_naam', 'placeholder' => '1. Naam van het cadeau', 'size'=>25));?></td>
        </tr>

         <tr>
          <td><?php echo($selectField);?></td>
        </tr> 

         <tr>
          <td><?php echo form_input(array('id'=>'ophaal_plaats', 'name'=>'ophaal_plaats', 'placeholder' => '3.Kies een stad', 'size'=>25));?></td>
        </tr>

        <div class="checkbox">
        <label><input type="checkbox" value="">Gebruik adres van mijn account</label>
        </div>

        <tr>
        <td>
            <h4>Upload foto</h4>
        <input type="file" name="userfile" />
          </td>
        </tr>

    <tr>
    <td><?php echo form_textarea(array('type'=>'textarea','id'=>'product_beschrijving', 'name'=>'product_beschrijving', 'placeholder' => '5. Vertel iets over dit cadeau..', 'size'=>25));?></td>
    </tr>


<tr>
<td><input type="submit" class="btn btn-primary" name="submit" value="Cadeau aanbieden!" /></td>
</tr>
     </table>
    </form>

我希望用户上传的每张图片都是400宽度和400高度,但我该怎么做?

2 个答案:

答案 0 :(得分:1)

使用此:

public function upload(){

$this->load->library('upload');
$this->load->library('image_lib');

        $config['upload_path'] = './upload/';
        $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
        $config['max_size']    = '0';
        $config['max_width']  = '0';
        $config['max_height']  = '0';
        $config['encrypt_name']= true;
        $this->upload->initialize($config);


if(!$this->upload->do_upload('userfile')){
    $error = array('error'=>$this->upload->display_errors());
    $this->load->view('product_form', $error);
}else{

    $data = $this->upload->data();
    $config['image_library'] = 'gd2';
    $config['source_image'] = './upload/'.$data["raw_name"].$data['file_ext'];
    $config['new_image'] = './upload/'.$data["raw_name"].$data['file_ext'];
    $config['create_thumb'] = FALSE;
    $config['maintain_ratio'] = FALSE;
    $config['width']         = 400;
    $config['height']       = 400;

    $this->image_lib->initialize($config);

    $this->image_lib->resize();


    $this->db->insert('products', array(
        'product_foto' => $data["raw_name"].$data['file_ext'],
        'product_naam'  => $this->input->post('product_naam'),
        'product_beschrijving' => $this->input->post('product_beschrijving'),
        'product_categorie'  => $this->input->post('product_categorie'),
        'ophaal_plaats'  => $this->input->post('ophaal_plaats'), 
        'date_created' => date('Y-m-d'),
        'date_updated' => date('Y-m-d')
        ));
    $data['img'] = base_url().'/upload/'.$data["raw_name"].$data['file_ext'];
    header('location:https://kadokado-ferran10.c9users.io/Product/');
}

答案 1 :(得分:0)

调用库:

$this->load->library('image_lib');

处理图像(上传图像后添加此代码)

$config['image_library'] = 'gd2';
$config['source_image'] = './upload/'.$file_data["file_name"];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width']         = 75;
$config['height']       = 50;
$this->load->library('image_lib', $config);

$this->image_lib->resize();