首页 文章

CodeIgniter上传并调整图像大小

提问于
浏览
0

我需要这个来上传和调整图像大小 . 我一直收到404错误,不知道为什么 . 它也假设在成功页面上输出图像,但我没有那么胖 . 我不太擅长codeignighter任何帮助表示赞赏 .

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

controller

<?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);
            }
    }
   }

views/form

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

    <?php echo $error;?>

    <form>

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

    

<input type="submit" value="upload" /> </form> </body> </html>

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

相关问题