我应该如何显示图像的拇指?其中image存储在db中,thumb存储在文件夹中

时间:2013-07-10 21:40:43

标签: php mysql codeigniter

我的控制器

<?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;?>

我将图像存储在数据库中以及文件夹中。我想通过单击图像的缩略图从数据库中检索图像。缩略图存储在文件夹中。错误是我无法显示缩略图,表示“未定义变量拇指”。我应该如何显示图像的拇指?我在很多方面尝试过,但无法得到结果。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

您未在$thumbs

中传递image_view()

有很多方法可以改进您的代码,并遵循更好的做法 - 但为了回答您的问题,用以下内容替换image_view()将清除您的错误:

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