CodeIgniter上载和调整图像大小

prdp8dxp  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(173)

我需要这个上传和调整大小的图像。我一直得到一个404错误,不知道为什么。它也应该输出的成功页面上的图像,但我还没有得到脂肪。我不是很擅长在代码点火器任何帮助是感激。
已知问题我在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>

视图/表单_成功

<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>
gijlo24d

gijlo24d1#

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

相关问题