2014-10-11 58 views
1

我一直在嘗試上傳和調整圖像大小。上傳似乎終於有效,但不是調整大小。Codeigniter文件調整上傳

在我的構造函數中我加載庫

$this->load->library('image_lib'); 

,然後在我的功能我打電話調整大小並上傳

$this->require_auth();  
    $img = $this->input->post('photo1'); 

    $config['upload_path'] = './HTML/files/'; 
    $config['file_name'] = 'photo1.jpg'; 
    $config['file_path'] = './HTML/files/photo1.jpg'; 
    $config['max-size'] = 2000; 
    $config['image_library'] = 'gd2'; 
    $config['source_image'] = $config['file_path']; 
    $config['create_thumb'] = TRUE; 
    $config['maintain_ratio'] = FALSE; 
    $config['width'] = 549; 
    $config['height'] = 549; 
    $config['overwrite'] = TRUE; 
    $config['allowed_types'] = 'gif|jpg|png|jpeg'; 
    $config['new_image'] = $config['file_path']; 
    $this->load->library('image_lib', $config); 
    $this->image_lib->resize(); 
    $this->load->library('upload', $config);     


    if (! $this->upload->do_upload('photo1')){   
     $error = array('error' => $this->upload->display_errors()); 
     var_dump($error); 
    }else{ 
     $this->blog_model->editServiceImg1($config['file_path']); 
     redirect('/blog'); 
    } 

我也試過這種

$this->require_auth();  
    $img = $this->input->post('service_photo1'); 

    $config['upload_path'] = './HTML/files/'; 
    $config['file_name'] = 'service_photo1.jpg'; 
    $config['file_path'] = './HTML/files/service_photo1.jpg'; 
    $config['max-size'] = 2000; 
    $config['overwrite'] = TRUE; 
    $config['allowed_types'] = 'gif|jpg|png|jpeg'; 
    $this->load->library('upload', $config);     


    if (! $this->upload->do_upload('service_photo1')){   
     $error = array('error' => $this->upload->display_errors()); 
     var_dump($error); 
    }else{ 
     $config['image_library'] = 'gd2'; 
     $config['source_image'] = $config['file_path']; 
     $config['create_thumb'] = TRUE; 
     $config['maintain_ratio'] = FALSE; 
     $config['width'] = 549; 
     $config['height'] = 549; 
     $config['new_image'] = $config['file_path']; 
     $this->image_lib->initialize($config); 
     $this->image_lib->resize(); 


     $this->blog_model->editServiceImg1($config['file_path']); 
     redirect('/blog'); 
    } 

有什麼我失蹤?我已經嘗試在初始上傳後調整大小,這似乎也無能爲力。任何幫助將非常感激。

+0

您是否嘗試在上傳圖片之前調整大小? – Xrymz 2014-10-11 14:55:23

+0

@LionKing不幸的是,它仍然不想工作。 – zazvorniki 2014-10-11 14:57:29

+0

@Xrymz,是的,那正是我想要完成的。 – zazvorniki 2014-10-11 14:57:54

回答

0

我通過這個代碼,去了很多次,終於解決了這個問題。看來,當你在你的代碼有

$config['create_thumb'] = TRUE; 

它會創建與調整比例的縮略圖,如果你不告訴它去哪裏它會隱藏的文件夾。所以調整大小工作,只是不正確的形象。

要解決這個問題,我改變了上面的。

$config['create_thumb'] = FALSE; 
0
$this->load->library('image_lib'); 
$config['image_library'] = 'gd2'; 
$config['allowed_types'] = 'gif|jpg|png|jpeg'; 
$config['upload_path'] = './HTML/files/'; 
$config['file_path'] = './HTML/files/photo1.jpg'; 
$config['source_image'] = $config['file_path']; 
$config['create_thumb'] = TRUE; 
$config['maintain_ratio'] = TRUE; 
$config['width']  = 75; 
$config['height'] = 50; 

$this->image_lib->clear(); 
$this->image_lib->initialize($config); 
$this->image_lib->resize(); 

如果上述代碼不起作用,請檢查文件夾上傳圖片的權限。

你也可以給777權限的文件夾中的下列方式: -

sudo chmod -R 777 path

解決方案一定會幫助你

+0

我其實發現它有什麼問題,而不是權限。我可以在該文件夾中寫入文件,但他們只是不調整大小。這個問題碰巧是我把拇指變成了真的。所以它正在調整大小,並在應用程序中的其他位置創建另一個文件,而不是在我需要的文件夾或正確的文件中。只要我把拇指設置爲假,那麼它就可以很好地工作。 – zazvorniki 2014-10-14 13:55:35