2013-07-10 28 views
0

我控制器我應該如何顯示圖像的拇指?其中形象店db和拇指店在文件夾

<?php class Image_control extends CI_Controller{ 
var $gallery_path; 
var $gallery_path_url; 
function index() 
{ 
    $this->load->view('image_view'); 
    //$this->do_upload(); 
} 

function __construct() 
{ 
    parent::__construct(); 

    $this->gallery_path= realpath(APPPATH . '../images1'); 
    $this->gallery_path_url=base_url().'images1/'; 
} 

function do_upload() 
{ 
    if($this->input->post('upload')) 
    { 
    $config = array(
     'allowed_types' => 'jpg|png|bmp', 
     'upload_path'=>$this->gallery_path, 
     'max_size'=>2000 
    ); 
    //echo $this->gallery_path; 

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

    if (!$this->upload->do_upload()) { 
     $errors[]=array('error'=>$this->upload->display_errors()); 
     $this->load->view('image_view',$errors); 
     //echo print_r($error); 
    } 

    $image_path=$this->upload->data(); 
    $file_name=$image_path['file_name']; 
    //$full_path=$image_path['full_path']; 

    $config = array(
     'a_name' => $this->input->post('a_name'), 
     'a_details'=>$this->input->post('a_info'), 
     'a_photo'=>$file_name 
    ); 

    $insert=$this->db->insert('animalstore',$config); 
    //return $insert; 

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

    $config = array(
     'source_image' => $image_data['full_path'], 
     'new_image'=>$this->gallery_path . './thumbs', 
     'maintain_ration'=>true, 
     'width'=>100, 
     'height'=>100 
    ); 

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

    $this->thumb_view(); 
    $this->image_view(); 

    //$this->load->view('image_view'); 
} 
} 

function get_thumbs() 
{ 
    $files=scandir($this->gallery_path); 
    $files=array_diff($files,array('.','..','thumbs')); 

    $images = array(); 

    foreach ($files as $file) { 
     $images[] = array(
      //'url' =>$this->gallery_path_url .$file, 
      'thumb_url'=>$this->gallery_path_url .'thumbs/'.$file 
     ); 
    } 

    return $images; 
} 

function thumb_view() 
{ 
    $data['thumbs']=$this->get_thumbs(); 
    $this->load->view('image_view',$data); 
} 

function get_images() 
{ 
    $query = $this->db->get('animalstore'); 

    /*$files=scandir($this->gallery_path); 
    $files=array_diff($files,array('.','..','thumbs')); 
    $images = array();*/ 

    if($query->num_rows() > 0) 
    { 
     foreach($query->result() as $rows) 
     { 
      /*$images[] = array(
       'url' => $this->gallery_path_url . $rows['url'], 
       'thumb_url' => $this->gallery_path_url . 'thumbs/' . $rows['url'] 
      );*/ 
      $data[] = $rows; 
     } 
     return $data; 
     //return $images; 
    } 

} 

function image_view() 
{ 
    $data['images']=$this->get_images(); 
    $this->load->view('image_view',$data); 
}}?> 

我看來

<html><body> 
<?php 
    echo form_open_multipart('image_control/do_upload'); 
    echo form_input('a_name','Animal Name'); 
    echo form_input('a_info','Animal Information'); 
    echo form_upload('userfile'); 
    echo form_submit('upload','Upload'); 
    echo form_close(); 
?> 

<?php if(isset($images) && count($images)): ?> 
    <?php foreach ($images as $image):?>     
     <h1><?php echo $image->a_name;?></h1> 
     <h1><?php echo $image->a_details;?></h1> 
     <a href="http://localhost/ci_test/images1/<?php echo $image->a_photo;?>"> 
      <img src="<?php echo $thumb['thumb_url'];?>"/> 
     </a>    
    <?php endforeach; ?> 
<?php else: ?> 
    <div id="blank_gallery">Please Upload an image</div> 
<?php endif;?> 

我將圖像存儲在數據庫中,並在一個文件夾中。我想通過點擊圖像的縮略圖從數據庫中檢索圖像。縮略圖存儲在文件夾中。錯誤是我無法顯示縮略圖它說「未定義的變量拇指」。我應該如何顯示圖像的拇指?我在很多方面嘗試過,但無法獲得結果。 在此先感謝。

+0

不將圖像存儲在數據庫,除非你有一個真正的特定需要 – 2013-07-10 21:44:47

+0

EM JST在DB @Dagon – brijeshp09

回答

0

您還沒有image_view()

傳遞$thumbs有很多方法你的代碼可以得到改善,更好的實踐遵循 - 但在回答你的問題,與下面將清除錯誤更換image_view()的利益:

function image_view() 
{ 
    $data['thumbs']=$this->get_thumbs(); 
    $data['images']=$this->get_images(); 
    $this->load->view('image_view',$data); 
} 
+0

其仍顯示誤差爲未定義索引存儲圖像名稱:thumb_url。 – brijeshp09