簡體   English   中英

CodeIgniter 上傳和調整圖片大小

[英]CodeIgniter upload and resize image

我需要這個來上傳和調整圖像大小。 我不斷收到 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>
$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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM