CodeIgniter上传并调整图片大小

时间:2017-12-08 06:09:55

标签: codeigniter image-manipulation codeigniter-upload

我需要这个来上传和调整图片大小。我一直收到404错误而且不知道为什么。它也假设在成功页面上输出图像,但我没有那么胖。我对codeignighter不太了解,感谢任何帮助。

已知问题我在MySQL中放入图像的表称为image_upload。我知道这从未被引用过,我现在知道我假设在构造中有一个$this->load->database();

控制器

<?php

class Upload extends CI_Controller {

   public function __construct()
    {
            parent::__construct();
            $this->load->helper(array('form', 'url'));
    }

    public function index()
    {
            $this->load->view('upload_form', array('error' => ' ' ));
    }

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

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

         $this->upload->do_upload('image');
    $fInfo = $this->upload->data(); // get all info of uploaded file

    //for image resize
    $img_array = array();
    $img_array['image_library'] = 'gd2';
    $img_array['maintain_ratio'] = TRUE;
    $img_array['create_thumb'] = TRUE;
    //you need this setting to tell the image lib which image to process
    $img_array['source_image'] = $fInfo['full_path'];
    $img_array['width'] = 113;
    $img_array['height'] = 75;

    $this->image_lib->clear(); // added this line
    $this->image_lib->initialize($img_array); // added this line
    if (!$this->image_lib->resize())




            if ( ! $this->upload->do_upload('userfile'))
            {
                    $error = array('error' => $this->upload->display_errors());

                    $this->load->view('upload_form', $error);
            }
            else
            {
                    $data = array('upload_data' => $this->upload->data());

                    $this->load->view('upload_success', $data);
            }
    }
   }

视图/形式

    <html>
    <head>
    <title>Upload Form</title>
    </head>
    <body>

    <?php echo $error;?>

    <form>

    <input type="file" name="userfile" size="20" />

    <br /><br />

    <input type="submit" value="upload" />

    </form>

    </body>
     </html>

视图/ form_success

    <html>
    <head>
    <title>Upload Form</title>
    </head>
    <body>

    <h3>Your file was successfully uploaded!</h3>

    <ul>
    <?php foreach ($upload_data as $item => $value):?>
    <li><?php echo $item;?>: <?php echo $value;?></li>
    <?php endforeach; ?>
    </ul>

     <p><?php echo anchor('upload', 'Upload Another File!'); ?></p>

    </body>
    </html>

1 个答案:

答案 0 :(得分:0)

$this->upload->do_upload('image');

将其更改为输入类型文件名

$this->upload->do_upload('userfile');

如果图像成功上传,则放置图片大小调整代码

if(!$this->upload->do_upload('userfile'))
{
    $fInfo = $this->upload->data(); // get all info of uploaded file
    //for image resize
    $img_array = array();
    $img_array['image_library'] = 'gd2';
    $img_array['maintain_ratio'] = TRUE;
    $img_array['create_thumb'] = TRUE;
    //you need this setting to tell the image lib which image to process
    $img_array['source_image'] = $fInfo['full_path'];
    $img_array['width'] = 113;
    $img_array['height'] = 75;

    $this->image_lib->clear(); // added this line
    $this->image_lib->initialize($img_array); // added this line
    $data = array('upload_data' => $this->upload->data());
    $this->load->view('upload_success', $data);
}
else
{
    $data = array('upload_data' => $this->upload->data());
    $this->load->view('upload_success', $data);
}