Codeigniter调整图像大小不起作用

时间:2014-07-08 12:28:21

标签: php codeigniter image-processing image-resizing

我正在尝试将上传的图像调整为163px的高度,保持宽高比,然后将其上传到文件夹。我尝试使用以下代码:

$id=1;     // user id
$this->load->library('image_lib');
$filename=$_FILES['file']['name'];
$config['image_library'] = 'gd2';
$config['upload_path'] = './userdata/'.$id;
$config['height'] = '163px';
$config['maintain_ratio'] = TRUE;
//$config['master_dim'] = 'height';
$config['source_image'] = $filename;
$this->load->library('upload', $config);
$this->image_lib->initialize($config);
$this->image_lib->resize();
if(!$this->upload->do_upload('file')) 
{               
     echo $this->data['error'] = $this->upload->display_errors();
}

然而,这是将图像上传到正确的文件夹,但图像未调整大小。我上传了一张大小为* 170 * 128 *的图片,并将其上传到文件夹,而不是调整大小。我的代码出了什么问题?

任何人都可以帮我找到问题吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

试试这个更干净的版本,高度或宽度的配置不需要px,因此你的代码有点令人困惑:

$id = 1;

$config = array(
    'upload_path'   => './userdata/'.$id,
    'allowed_types' => 'gif|jpg|jpeg|png',
    'encrypt_name'  => true,
);

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

$field_name = "file"; // change this with your file upload part's field name if different

if ($this->upload->do_upload($field_name)) {
    $image = $this->upload->data();
    $config = array(
        'image_library'   => 'gd2',
        'source_image' => $image['full_path'],
        'maintain_ratio'  => true,
        'height'  => 163,
     );
     $this->load->library('image_lib', $config);
     if (!$this->image_lib->resize()) {
            echo $this->image_lib->display_errors();
     }
     $this->image_lib->clear();
}