Function do_upload() berfungsi untuk mengupload image ke server sekaligus membuat multiple thumbnail dengan memanggil function _create_thumbs().
Function _create_thumbs() berfungsi untuk membuat multiple thumbnail.
Buat sebuah file Controller pada folder “application/controllers” dengan nama Upload.php, kemudian ketikan kode berikut:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | <?php class Upload extends CI_Controller{ function __construct(){ parent::__construct(); $this->load->library('upload'); } function index(){ $this->load->view('upload_view'); } function do_upload(){ $config['upload_path'] = './assets/images/'; //path folder $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp'; //type yang image yang dizinkan $config['encrypt_name'] = TRUE; //enkripsi nama file $this->upload->initialize($config); if(!empty($_FILES['filefoto']['name'])){ if ($this->upload->do_upload('filefoto')){ $gbr = $this->upload->data(); //Compress Image $this->_create_thumbs($gbr['file_name']); $this->session->set_flashdata('msg','<div class="alert alert-info">Image Berhasil di upload.</div>'); redirect('upload'); }else{ echo $this->upload->display_errors(); } }else{ echo "image kosong atau type image tidak"; } } function _create_thumbs($file_name){ // Image resizing config $config = array( // Image Large array( 'image_library' => 'GD2', 'source_image' => './assets/images/'.$file_name, 'maintain_ratio'=> FALSE, 'width' => 700, 'height' => 467, 'new_image' => './assets/images/large/'.$file_name ), // image Medium array( 'image_library' => 'GD2', 'source_image' => './assets/images/'.$file_name, 'maintain_ratio'=> FALSE, 'width' => 600, 'height' => 400, 'new_image' => './assets/images/medium/'.$file_name ), // Image Small array( 'image_library' => 'GD2', 'source_image' => './assets/images/'.$file_name, 'maintain_ratio'=> FALSE, 'width' => 100, 'height' => 67, 'new_image' => './assets/images/small/'.$file_name )); $this->load->library('image_lib', $config[0]); foreach ($config as $item){ $this->image_lib->initialize($item); if(!$this->image_lib->resize()){ return false; } $this->image_lib->clear(); } } } |