编码器调整图像的大小不工作


Codeigniter resizing of image not working

我试图将上传的图像调整为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*的图像,它被上传到文件夹,因为它没有调整大小。我的代码有什么问题?

谁能帮我找出问题所在吗?

试试这个更简洁的版本,高度或宽度的配置不需要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();
}

Simple…

你只需要做一件事进入Captcha_helper.php搜索

不是

    $x = mt_rand(0, $img_width / ($length / 3));

Do Like this

    $x = mt_rand(0, $img_width / ($length / 1));