2017-08-30 46 views
0

我在CodeIgniter中创建了一个函数,因此当我添加产品的图片时,它会上传常规尺寸(product_foto)和缩略图尺寸(product_foto_thumb)。在“所有产品”页面上,图片都是我喜欢的缩略图尺寸,但是当我点击某个产品进入详细信息页面时,图片仍然很小,并且仍然与缩略图图片的尺寸相同,而我在该视图页面上加载常规图片。codeigniter加载了错误的图片尺寸

这是我上传功能在我的控制文件:

<div class="container"> 
<div class="row"> 
<div class="col-md-4"> 
<img src="<?php echo base_ur().'upload/'$product['product_foto'] ?>"> 

</div> 
</div> 

</div> 

我的缩略图视图文件:

<?php foreach($products as $product) : ?> 
<div class="col-lg-2"> 
<div id="product"> 
<a href="<?php echo base_url() ?>/Products/details/<?php echo $product['product_id']; ?>"> 
<?php echo '<img src="upload/'.$product['product_naam']; ?> </div> 
</div> 

<?php endforeach; ?> 

public function upload(){ 

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

      $config['upload_path'] = './upload/'; 
      $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg'; 
      $config['max_size'] = '0'; 
      $config['max_width'] = '0'; 
      $config['max_height'] = '0'; 
      $config['encrypt_name']= true; 
      $this->upload->initialize($config); 


    if(!$this->upload->do_upload('userfile')){ 
     $error = array('error'=>$this->upload->display_errors()); 
     $this->load->view('product_form', $error); 
    }else{ 
     //Main image 
     $data = $this->upload->data(); 
     $config['image_library'] = 'gd2'; 
     $config['source_image'] = './upload/'.$data["raw_name"].$data['file_ext']; 
     $config['new_image'] = './upload/'.$data["raw_name"].$data['file_ext']; 
     $config['create_thumb'] = FALSE; 
     $config['maintain_ratio'] = FALSE; 
     $config['width'] = 640; 
     $config['height'] = 380; 

     //Thumb image 
     $dataThumb = $this->upload->data(); 
     $configThumb['image_library'] = 'gd2'; 
     $configThumb['source_image'] = './upload/'.$dataThumb["raw_name"].$dataThumb['file_ext']; 
     $configThumb['new_image'] = './upload/'.$dataThumb["raw_name"].$dataThumb['file_ext']; 
     $configThumb['create_thumb'] = TRUE; 
     $configThumb['maintain_ratio'] = TRUE; 
     $configThumb['width'] = 168; 
     $configThumb['height'] = 150; 

     $this->image_lib->initialize($config); 
     $this->image_lib->initialize($configThumb); 

     $this->image_lib->resize(); 


     $this-> db-> insert('products', array(
      'product_foto' => $data["raw_name"].$data['file_ext'], 
      'product_foto_thumb' => $dataThumb["raw_name"].$dataThumb['file_ext'], 

      'product_naam' => $this->input->post('product_naam'), 
      'product_beschrijving' => $this->input->post('product_beschrijving'), 
      'product_categorie' => $this-> input->post('product_categorie'), 
      'ophaal_plaats' => $this->input->post('ophaal_plaats'), 
      'date_created' => date('Y-m-d'), 
      'date_updated' => date('Y-m-d') 
     )); 
     $data['img'] = base_url(). 
     '/upload/'.$data["raw_name"].$data['file_ext']; 
     $dataThumb['img'] = base_url(). 
     '/upload/'.$dataThumb["raw_name"].$dataThumb['file_ext']; 

     header('location:https://www.example.com/Product/'); 
} 

在那里我装载大尺寸的图片我的视图文件

+0

我们展示拇指视图代码。 –

+0

好的,我编辑它的人。您现在可以在我的问题中看到缩略图视图 – lablanco

+0

您可以检查它吗?我编辑了我的问题 – lablanco

回答

0

请参阅下面的代码。我认为它肯定会解决你的问题。

public function upload() { 
     $this->load->library('upload'); 
     $this->load->library('image_lib'); 

     $config['upload_path'] = './upload/'; 
     $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg'; 
     $config['max_size'] = '0'; 
     $config['max_width'] = '0'; 
     $config['max_height'] = '0'; 
     $config['encrypt_name']= true; 
     $this->upload->initialize($config); 


     if(!$this->upload->do_upload('userfile')){ 
      $error = array('error'=>$this->upload->display_errors()); 
      $this->load->view('product_form', $error); 
     }else{ 
      //Main image 
      $data = $this->upload->data(); 
      $config['image_library'] = 'gd2'; 
      $config['source_image'] = './upload/'.$data["raw_name"].$data['file_ext']; 
      $config['new_image'] = './upload/'.'new_'.$data["raw_name"].$data['file_ext']; 
      $config['create_thumb'] = FALSE; 
      $config['maintain_ratio'] = FALSE; 
      $config['width'] = 640; 
      $config['height'] = 380; 

      //Thumb image 
      $dataThumb = $this->upload->data(); 
      $configThumb['image_library'] = 'gd2'; 
      $configThumb['source_image'] = './upload/'.$dataThumb["raw_name"].$dataThumb['file_ext']; 
      $configThumb['new_image'] = './upload/'.'thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext']; 
      $configThumb['create_thumb'] = TRUE; 
      $configThumb['maintain_ratio'] = TRUE; 
      $configThumb['width'] = 168; 
      $configThumb['height'] = 150; 

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

      $this->image_lib->initialize($configThumb); 
      $this->image_lib->resize(); 

      $this-> db-> insert('products', array(
       'product_foto' => 'new_'.$data["raw_name"].$data['file_ext'], 
       'product_foto_thumb' => 'thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'], 

       'product_naam' => $this->input->post('product_naam'), 
       'product_beschrijving' => $this->input->post('product_beschrijving'), 
       'product_categorie' => $this-> input->post('product_categorie'), 
       'ophaal_plaats' => $this->input->post('ophaal_plaats'), 
       'date_created' => date('Y-m-d'), 
       'date_updated' => date('Y-m-d') 
      )); 
      $data['img'] = base_url(). 
      '/upload/new_'.$data["raw_name"].$data['file_ext']; 
      $dataThumb['img'] = base_url(). 
      '/upload/thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext']; 

      header('location:https://kadokado-ferran10.c9users.io/Product/'); 
     } 
    } 
} 

拇指代码示例

<?php foreach($products as $product) : ?> 
<div class="col-lg-2"> 
    <div id="product"> 
     <img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto_thumb']; ?>"> 
    </div> 
</div> 
<?php endforeach; ?> 

你的主要形象的例子

<?php foreach($products as $product) : ?> 
<div class="col-lg-2"> 
    <div id="product"> 
     <img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto']; ?>"> 
    </div> 
</div> 
<?php endforeach; ?> 
+0

这个答案解决了你的问题吗? –

+0

你好,我添加了你改变的东西,它仍然是一样的:( – lablanco

+0

)请清除你的上传目录,然后上传一张照片,然后看看有多少文件在那里,他们的名字是什么 –