如何调整图像大小并在codeigniter中创建缩略图

时间:2013-09-21 10:47:50

标签: codeigniter

我有在codeigniter上传图像的功能,在这里我可以上传任何图像,但是当我需要在我的网站上显示这些图像时,我将使用此代码: -

<img src="uploads/<?=$user->pic?>" width="250px" height="200px" />

如果图像大于250,图像显示不好。

那么我怎么能为我的照片做拇指。

public function do_upload($field) {
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '1000';
    $config['max_width'] = '1024';
    $config['max_height'] = '768';

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

    if (!$this->upload->do_upload($field)) 
    {
        $error = array('error' => $this->upload->display_errors());
        return $error;
    } 
    else
    {
      $updata =$this->upload->data();
      $data = $updata['raw_name'].$updata['file_ext'];
      return $data;
    }
}

1 个答案:

答案 0 :(得分:1)

在这里,你只需指定file_path就可以了(+宽度/高度) 如需更多选项,请查看here

public function resize_image($file_path, $width, $height) {

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

    $img_cfg['image_library'] = 'gd2';
    $img_cfg['source_image'] = $file_path;
    $img_cfg['maintain_ratio'] = TRUE;
    $img_cfg['create_thumb'] = TRUE;
    $img_cfg['new_image'] = $file_path;
    $img_cfg['width'] = $width;
    $img_cfg['quality'] = 100;
    $img_cfg['height'] = $height;

    $this->image_lib->initialize($img_cfg);
    $this->image_lib->resize();

}