簡體   English   中英

在刷新之前,無法在Codeigniter中上載和調整大小的照片在視圖中可用..視圖加載是否“過快”?

[英]Uploaded and resized photo in Codeigniter is not available in view until I refresh .. is my view loading “too fast”?

我在Codeigniter中的照片上傳系統遇到問題。

我正在控制器中注冊期間上傳照片並調整其大小,一旦完成,我將加載“成功上傳”視圖,在此我想再次顯示照片。

問題是,

  • 我進入實際上似乎沒有失敗的“失敗”循環

  • 第一次加載視圖時,該圖片未顯示(盡管它已上傳,調整大小,並且路徑在我的數據庫中)。 但是,如果刷新頁面,它會正確顯示。

這是控制器..檢查靠近底部的注釋,因為頂部基本上只是調整大小的邏輯,並且(盡管現在有點難看)它確實起作用

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

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

    if ( $this->upload->do_upload() )
    {
        $this->data['upload_data'] = $this->upload->data();
        $this->data['title'] = "Upload Successful";

         switch($this->data['upload_data']['file_type']){
            case "image/jpg":
                $_file_suffix = ".jpg";
                break;
            case "image/jpeg":
                $_file_suffix = ".jpg";
                break;
            case "image/png":
                $_file_suffix = ".png";
                break;
            case "image/gif":
                $_file_suffix = ".gif";
                break;
            case "image/pjpeg":
                $_file_suffix = ".jpg";
                break;
         }

        $_fileName = $this->data['upload_data']['file_name']; 
        $_oldUploadPath = $config['upload_path'] . $_fileName;
        $_random_key = strtotime(date('Y-m-d H:i:s'));
        $_newUploadPath = $config['upload_path'] . 'resize_' . $_random_key . $_file_suffix;
        rename( $_oldUploadPath, $_newUploadPath );

        $_oldSize = getimagesize($_newUploadPath);
        $_oldWidth = $_oldSize[0];

        if($_oldWidth > $_maxResizedWidth){
            $_scale = $_maxResizedWidth/$_oldWidth;
            $_oldHeight = $_oldSize[1];
            $_imageType = image_type_to_mime_type($_oldSize[2]);
            $_newWidth = $_oldWidth * $_scale;
            $_newHeight = $_oldHeight * $_scale;
            $_resizedImage = imagecreatetruecolor($_newWidth, $_newHeight);
            switch($_imageType) {
                case "image/gif":
                    $_source=imagecreatefromgif($_newUploadPath); 
                    break;
                case "image/pjpeg":
                case "image/jpeg":
                case "image/jpg":
                    $_source=imagecreatefromjpeg($_newUploadPath); 
                    break;
                case "image/png":
                case "image/x-png":
                    $_source=imagecreatefrompng($_newUploadPath); 
                    break;
            }
            imagecopyresampled($_resizedImage, $_source, 0, 0, 0, 0, $_newWidth, $_newHeight, $_oldWidth, $_oldHeight);
            switch($_imageType) {
                case "image/gif":
                    imagegif($_resizedImage,$_newUploadPath); 
                    break;
                case "image/pjpeg":
                case "image/jpeg":
                case "image/jpg":
                    imagejpeg($_resizedImage,$_newUploadPath,90); 
                    break;
                case "image/png":
                case "image/x-png":
                    imagepng($_resizedImage,$_newUploadPath);  
                    break;
            }
        }

        $_newUploadPathWithoutDot = substr($_newUploadPath,1);

        $this->load->model('Member_model');
        $ID = $this->the_user->id;

             /// ****************************************************************************
             /// I step into this next if loop even though the upload_photo method does work.  
            /// In my view I get the "there was a problem" .. but the DB has been correctly updated

        if(!$this->Member_model->upload_photo( $ID, $_newUploadPathWithoutDot));
        {
            $uploadData = $this->upload->data();
            $this->data['message'] = "There was a problem entering your photo in the database ==";
        };

              /// And this is the view that's called
        $this->load->view('upload_big_success_view', $this->data);
    }
    else
    {
        $this->data['message'] = "there was a problem " . $this->upload->display_errors() . dirname(__FILE__);
        $this->data['title'] = "Upload Unsuccessful";
        $this->load->view('profile_photo_view', $this->data);
    }
}

(我知道里面有很多亂七八糟的東西,我將對其進行重構)。

首次加載視圖時,似乎一切似乎都還沒有准備好,即使一切都已完成-上傳,調整大小,文件路徑都已輸入到數據庫中……直到最后一次都沒有拋出任何錯誤。循環,似乎認為“-> upload_photo()”沒有失敗。 然后當我第一次調用視圖時

<img src"<?php echo $the_user->large_image_location; ?>" > 

..不產生任何輸出。 但是經過簡單的刷新,它就可以了。 盡管視圖中仍顯示“有問題消息”。

順便說一句-這是模型。 沒有什么花哨。

public function upload_photo($ID, $_newUploadPath)
{
    $sql = "UPDATE users SET large_image_location=? WHERE id=?";
    $query = $this->db->query($sql, array($_newUploadPath, $ID));
    return $query;  
}

有什么想法嗎?

加載視圖時,Codeigniter不會刷新頁面,因此它將永遠不會加載圖像,相反,您需要通過使用redirect()函數告訴Codeigniter刷新頁面。 而不是在do_upload()方法末尾加載成功視圖,您應該重定向到成功頁面。

我將創建一個upload_big_success()方法,為您加載您的upload_big_success_view ,然后重定向到該方法。


do_upload()替換為do_upload()

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

有:

redirect(upload_big_success, 'location');


然后在同一控制器中為成功頁面創建方法。

public function upload_big_success() {
   // And this is the view that's called
   $this->load->view('upload_big_success_view');
}

暫無
暫無

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

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