2016-05-18 190 views
0

我的控制器:如何上傳圖片,調整其大小以及如何在codeigniter中顯示調整大小的圖片?

public function addgalleryProcess() 
     { 


      $height = $this->input->POST('height'); 
      $width = $this->input->POST('width'); 


      $config['upload_path'] = './assets/images/gallery'; 
      $config['allowed_types'] = 'gif|jpg|png'; 
      $config['max_size'] = '1000'; 
      $config['max_width'] = ''; 
      $config['max_height'] = ''; 
      $config['overwrite'] = TRUE; 
      $config['remove_spaces'] = TRUE; 

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

      if (! $this->upload->do_upload()) 
      { 
        $error = array('error' => $this->upload->display_errors()); 
        $this->load->view('admin/addgallery', $error); 
      } 
      else 
      { 

       $upload_data = $this->upload->data(); 

       //resize: 

       $config1['image_library'] = 'gd2'; 
       $config1['source_image'] = $upload_data['full_path']; 
       $config1['maintain_ratio'] = TRUE; 
       $config1['create_thumb'] = TRUE; 
       $config1['width'] = $width; 
       $config1['height'] = $height; 
       $this->load->library('image_lib', $config1); 
       $this->image_lib->resize(); 

       $this->adminModel->galleryimages($upload_data); 
       $this->load->view('admin/homeView'); 
       } 

我的模型:

public function galleryimages($image_data = array()) 
{ 

    $data = array(
    'image' => $image_data['file_name'], 

    ); 

     $this->db->insert('gallery', $data); 
} 

這裏像正常工作和上傳,但調整不工作。我必須顯示指定寬度和高度的已調整大小的圖像。我是新來的。 預先感謝。

回答

0

試試這個:

對於圖像大小調整,我建議用 「TimThumb」 TimThumb - PHP圖像調整

https://www.binarymoon.co.uk/projects/timthumb/

我使用timthumb在我的項目像

<img src="<?php echo base_url('assets/common/timthumb') . '/timthumb.php?src=./assets/myuploads/myimagename.jpg&w=245&h=164'; ?>" alt=""> 

我已將timthumb.php放入「assets」文件夾中,並將我的圖片上載到「assets/myuploads」文件夾中

0

不要分心,如果要在通過網絡上傳視頻之前調整圖像大小,可以將它javascript調整爲畫布,然後將畫布圖像發送到toDataURL。這將有助於防止某人上傳大量內容,並使您的網絡和服務器與之爭鬥。 這也取決於您的客戶端是否在HTML客戶端中。 以下文章可以幫助您: https://stackoverflow.com/a/10334170/811827