-1

How would I insert "_thumb" before file extension ? for example 123.jpg into 123_thumb.jpg

please answer my question, it's been 3 days i didn't find the right way.. thank you

this is my view :

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

and this is my controller :

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();


            }
         }
      }
      
   }

and this is my Model :

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

2 Answers2

0

English is not my native language, sorry for that.

For set à new name for your thumb, you just change this config

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

When you do this you get the lot of informations about file

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

If you like inside the Ci::Upload, you see the function data return this

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

In your case what interests you it's

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

Which give :

                   $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: See this Codeigniter image resize() thumbnail name issue

Community
  • 1
  • 1
MaolmeoX
  • 46
  • 3
0

i use this extension : http://www.matmoo.com/digital-dribble/codeigniter/image_moo/

It's more successful than Codeigniter's own library. Especially when I want to save thumbnails in multiple sizes.