【问题标题】:Multiple file upload and resize in codeigniter在codeigniter中上传多个文件并调整大小
【发布时间】:2013-10-28 14:08:38
【问题描述】:

我有一个可以上传 3 张图片的表单。 在 codeigniter 中,我创建了一个 foreach 循环来处理图像,并将它们保存到我的数据库中。效果很好。

但我需要调整图像的大小。当我上传一张图片时,调整大小可以工作,但是超过一张图片,第一张图片看起来很糟糕,第二张和第三张图片不会调整大小......你们能帮帮我吗?

$this->load->library('upload');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
$config['encrypt_name'] = TRUE;
//$config['overwrite'] = TRUE;

$this->upload->initialize($config);
 $count = 0;
            foreach($_FILES as $field => $file)
            {
                $count = $count + 1;
                // No problems with the file
                if($file['error'] == 0)
                {
                    // So lets upload
                    if ($this->upload->do_upload($field))
                    {
                        $data = $this->upload->data();

                        $data2 = array(
               'image' . $count => $data['file_name']
            );

$this->db->where('id', $query_id);
$this->db->update('ads', $data2);
//afbeeldingen verkleinen.

                    $config['image_library'] = 'gd2';
                    $config['source_image'] = $data['full_path'];
                    //$config['new_image'] = './image_uploads/';
                    $config['maintain_ratio'] = TRUE;
                    $config['width'] = 370;
                    $config['height'] = 277;
                    $this->load->library('image_lib',$config); 

                    if ( !$this->image_lib->resize()){
                $this->session->set_flashdata('errors', $this->image_lib->display_errors('', '')); 
                echo $this->image_lib->display_errors('', '');
                exit();  
              }

                    }
                    else
                    {
                        $errors = $this->upload->display_errors();
                        echo $errors;
                        exit();
                    }
                }
            }
$data['name'] = $name;
redirect('/adDetail/getDetails/' . $query_id, 'location', 200);
}

【问题讨论】:

  • 尝试重置$config变量

标签: php image codeigniter upload image-resizing


【解决方案1】:

尝试在循环结束时卸载 image_lib。

【讨论】:

  • 现在,当我上传 2 个文件时,第一个文件的大小已正确调整。对于第二个文件,我收到错误:您的服务器不支持处理此类图像所需的 GD 功能。但是我尝试上传的两张图片是一样的......
  • 您每次都在循环中重新加载库。正如我所说,尝试在最后卸载它,这样它就不会加载多次。
猜你喜欢
  • 2015-07-30
  • 2013-06-01
  • 1970-01-01
  • 2015-06-19
  • 1970-01-01
  • 2011-05-18
  • 1970-01-01
  • 1970-01-01
  • 2011-08-05
相关资源
最近更新 更多