2017-05-09 85 views
-1

如何在文件擴展名之前插入「_thumb」? 例如123.jpg到123_thumb.jpg如何在文件擴展名之前插入「_thumb」

請回答我的問題,這是3天,我沒有找到合適的方式..謝謝

這是我的看法:

<div class="row service-box margin-bottom-40"> 
 
        <!-- carousel --> 
 
        <div class="col-md-4"> 
 
         <div class="carousel slide"> 
 
          <?php 
 
          $reports_slide = $this->m_dashboard->report_slide(); 
 
          foreach ($reports_slide as $row) { 
 
           echo ' 
 
          <img width="100%" src="' . base_url() . 'img/report/thumbs/' . $row->report_img . '" class="mySlides"> 
 
           '; 
 
          } ?> 
 
          <script> 
 
           var slideIndex = 0; 
 
           carousel(); 
 

 
           function carousel() { 
 
            var i; 
 
            var x = document.getElementsByClassName("mySlides"); 
 
            for (i = 0; i < x.length; i++) { 
 
             x[i].style.display = "none"; 
 
            } 
 
            slideIndex++; 
 
            if (slideIndex > x.length) {slideIndex = 1} 
 
            x[slideIndex-1].style.display = "block"; 
 
            setTimeout(carousel, 2000); // Change image every 2 seconds 
 
           } 
 
          </script> 
 
         </div> 
 
        </div>

,這是我的控制器:

public function add_report() 
 
    { 
 

 
     $field_img = "img"; 
 
     $field_thumb = "thumb"; 
 
     $field_pdf = "pdf"; 
 
     // this is for form field 1 which is an image.... 
 
     $config['upload_path'] = '../img/report/'; 
 
     $config['allowed_types'] = 'gif|jpg|png|pdf'; 
 
     $config['max_size'] = '1024'; 
 
     $config['remove_spaces'] = TRUE; 
 
     $config['overwrite'] = TRUE; 
 

 
     $this->load->view('includes/header'); 
 
     $this->load->view('includes/menu'); 
 
     if (isset($_POST['submit'])) { 
 
      if (isset($_FILES['img']['name']) && is_uploaded_file($_FILES['img']['tmp_name'])){ 
 
       $config['file_name'] = '_image_'.$_POST['id_company'].'_'.$_POST['report_title']; 
 
       $this->upload->initialize($config); 
 
       $this->upload->do_upload($field_img); 
 
       $file_name = $this->upload->data(); 
 
       $file_img = $file_name['file_name']; 
 

 
       $config = array(
 
        'image_library' => 'gd2', 
 
        'source_image' => $file_name['full_path'], 
 
        'new_image'  => '../img/report/thumbs/', 
 
        'create_thumb' => TRUE, 
 
        'maintain_ratio' => FALSE, 
 
        'width'   => 827, 
 
        'height'   => 1170, 
 
       ); 
 
       $this->image_lib->clear(); 
 
       $this->image_lib->initialize($config); 
 
       $this->image_lib->resize(); 
 

 

 
      } 
 
     } 
 
     } 
 
     
 
    }

,這是我的模型:

public function report_slide() 
 
    { 
 
     //report 
 
     $this->db->select('a.*, b.category_name, c.co_abbreviated_name, d.country_name, e.language, f.report_type, g.year, h.type'); 
 
     $this->db->from('rc_report a, rc_category b, rc_company c, rc_country d, rc_language e, rc_report_type f, rc_year g, rc_gri h'); 
 
     $this->db->where('b.id_category = a.id_category'); 
 
     $this->db->where('c.id_company = a.id_company'); 
 
     $this->db->where('d.id_country = a.id_country'); 
 
     $this->db->where('e.id_language = a.id_language'); 
 
     $this->db->where('f.id_report_type = a.id_report_type'); 
 
     $this->db->where('g.id_year = a.id_year'); 
 
     $this->db->where('h.id_gri = a.id_gri'); 
 
     $this->db->order_by('g.year','desc'); 
 
     $this->db->limit(5); 
 
     $query_report = $this->db->get(); 
 
     $result_array=$query_report->result(); 
 

 
     return $result_array; 
 
    }

+1

您是否檢查過這個網址http://stackoverflow.com/questions/2429611/regex-how-to-insert-string-before-file-extension –

+0

如果您確定文件名中只有一個點,那麼你可以使用'str_replace()' – RST

+0

在哪裏應該把這種語法,在視圖或控制器? –

回答

0

英語不是我的母語,對不起那個。

對於設置了您的拇指新的名字,你只需要改變這個配置

'new_image'  => '../img/report/thumbs/' 

當你這樣做,你得到了很多信息的有關文件

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

如果你喜歡的詞裏面::上傳,你看到函數數據返回這個

public function data($index = NULL) 
{ 
    $data = array(
      'file_name'  => $this->file_name, 
      'file_type'  => $this->file_type, 
      'file_path'  => $this->upload_path, 
      'full_path'  => $this->upload_path.$this->file_name, 
      'raw_name'  => substr($this->file_name, 0, -strlen($this->file_ext)), 
      'orig_name'  => $this->orig_name, 
      'client_name'  => $this->client_name, 
      'file_ext'  => $this->file_ext, 
      'file_size'  => $this->file_size, 
      'is_image'  => $this->is_image(), 
      'image_width'  => $this->image_width, 
      'image_height'  => $this->image_height, 
      'image_type'  => $this->image_type, 
      'image_size_str' => $this->image_size_str, 
     ); 

    if (! empty($index)) 
    { 
     return isset($data[$index]) ? $data[$index] : NULL; 
    } 

    return $data; 
} 

在你的情況下你有什麼感興趣的是

   'raw_name'  => substr($this->file_name, 0, -strlen($this->file_ext)), 

這給:

    $config = array(
       'image_library' => 'gd2', 
       'source_image' => $file_name['full_path'], 
       'new_image'  => "../img/report/thumbs/".$file_name['raw_name']."_thumb.".$file_name['file_ext'], 
       'create_thumb' => TRUE, 
       'maintain_ratio' => FALSE, 
       'width'   => 827, 
       'height'   => 1170, 
      ); 

PS:請參閱本Codeigniter image resize() thumbnail name issue