【问题标题】:Codeigniter resizing of image not workingCodeigniter调整图像大小不起作用
【发布时间】:2014-08-29 04:37:42
【问题描述】:

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

$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为单位吗?或者你可以只使用“163”吗?
  • 应该是$config['height'] = '163'; remove px

标签: php codeigniter image-processing image-resizing


【解决方案1】:

试试这个更简洁的版本,高度或宽度的配置不需要像素,因此你的代码有点混乱:

$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();
}

【讨论】:

  • 我已经在我的本地尝试过,它的工作原理,你能通过简单的 phpinfo() 调用检查 gd 是否已安装或启用?
  • @Burak..我让它工作了。问题是我多次包含$this->load->library('image_lib')..
  • 很高兴听到您已修复。
【解决方案2】:

简单...

你必须做一件事去 Captcha_helper.php 并搜索

而不是

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

这样做

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

【讨论】:

    猜你喜欢
    • 2023-03-31
    • 2016-12-31
    • 1970-01-01
    • 2014-05-13
    • 2014-05-12
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多