HanzZame

Multiple Upload and Resize Images Codeigniter 3.0.4

Mar 2nd, 2016
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.74 KB | None | 0 0
  1. #CONTROLERS FUNGSI MULTI UPLOAD GAMBAR DAN MULTI RESIZE GAMBAR.
  2.  
  3.  
  4. public function proses_tambah()
  5.     {
  6.         $this->_rules(); //manggil fungsi validasi
  7.  
  8.         $kode = $this->Produk_model->get_kode(); // manggil fungsi id buat id relasi
  9.  
  10.         $tanggal  = date('Y-m-d'); // tanggal
  11.        
  12.        if ($this->form_validation->run() == FALSE) {
  13.             $this->tambah(); // kembali ke halaman tambah jika form ada yg kosong
  14.         }
  15.         else
  16.         {
  17.  
  18.                 $user_upload_path = 'media/produk/';
  19.                 $config['upload_path'] = './media/produk/ori/';
  20.                 $config['allowed_types'] = 'gif|jpg|png';
  21.                 $config['max_size']  = 1024 * 4;
  22.                 //$config['encrypt_name'] = TRUE;
  23.          
  24.                 $this->load->library('upload', $config);
  25.  
  26.                 $result_array = array();
  27.  
  28.             for ($i = 1; $i <=4; $i++){
  29.  
  30.                 if (!$this->upload->do_upload('gambar_produk'.$i))
  31.                 {
  32.                     $data['upload_error'] = $this->upload->display_errors();
  33.                 }
  34.                 else
  35.                 {
  36.                     array_push($result_array,$this->upload->data());
  37.  
  38.                     $data_upload = $this->upload->data();
  39.  
  40.                     $file_name = $data_upload["file_name"];
  41.                  
  42.  
  43.                     //merubah nama gambar menjadi seperti ini (470x470_nama_gambar.jpg).
  44.                     $file_name_thumb1 = '470x470_'.$data_upload['raw_name'].$data_upload['file_ext'];
  45.                     $file_name_thumb2 = '300x300_'.$data_upload['raw_name'].$data_upload['file_ext'];
  46.                     $file_name_thumb3 = '175x175_'.$data_upload['raw_name'].$data_upload['file_ext'];
  47.  
  48.                     $this->load->library('image_lib');
  49.  
  50.                     //resize1
  51.                     $config_resize1['image_library'] = 'gd2';    
  52.                     $config_resize1['maintain_ratio'] = TRUE;
  53.                     $config_resize1['master_dim'] = 'height';
  54.                     $config_resize1['quality'] = "100%";
  55.                     $config_resize1['source_image'] = './' . $user_upload_path.'ori/'. $file_name;
  56.                     $config_resize1['new_image'] = './' . $user_upload_path.'detail/' . $file_name_thumb1;
  57.                     $config_resize1['height'] = 470;
  58.                     $config_resize1['width'] = 470;
  59.                     $this->image_lib->initialize($config_resize1);
  60.                     $this->image_lib->resize();
  61.                     $this->image_lib->clear();
  62.  
  63.                     //resize2
  64.                     $config_resize2['image_library'] = 'gd2';    
  65.                     $config_resize2['maintain_ratio'] = TRUE;
  66.                     $config_resize2['master_dim'] = 'height';
  67.                     $config_resize2['quality'] = "100%";
  68.                     $config_resize2['source_image'] = './' . $user_upload_path.'ori/' . $file_name;
  69.                     $config_resize2['new_image'] = './' . $user_upload_path.'medium/' . $file_name_thumb2;
  70.                     $config_resize2['height'] = 300;
  71.                     $config_resize2['width'] = 300;
  72.                     $this->image_lib->initialize($config_resize2);
  73.                     $this->image_lib->resize();
  74.                     $this->image_lib->clear();
  75.  
  76.                     //resize3
  77.                     $config_resize3['image_library'] = 'gd2';    
  78.                     $config_resize3['maintain_ratio'] = TRUE;
  79.                     $config_resize3['master_dim'] = 'height';
  80.                     $config_resize3['quality'] = "100%";
  81.                     $config_resize3['source_image'] = './' . $user_upload_path.'ori/' . $file_name;
  82.                     $config_resize3['new_image'] = './' . $user_upload_path.'small/' . $file_name_thumb3;
  83.                     $config_resize3['height'] = 175;
  84.                     $config_resize3['width'] = 175;
  85.                     $this->image_lib->initialize($config_resize3);
  86.                     $this->image_lib->resize();
  87.                     $this->image_lib->clear();
  88.  
  89.                 }
  90.             }
  91.  
  92.             $data = array(
  93.                     'sku' => $kode,
  94.                     'nama_produk' => $this->input->post('nama_produk',TRUE),
  95.                     'model_tipe' => $this->input->post('model_tipe',TRUE),
  96.                     'stok' => $this->input->post('stok',TRUE),
  97.                     'harga_jual' => $this->input->post('harga_jual',TRUE),
  98.                     'deskripsi' => $this->input->post('deskripsi',TRUE),
  99.                     'spesifikasi' => $this->input->post('spesifikasi',TRUE),
  100.                     'id_kategori' => $this->input->post('id_kategori',TRUE),
  101.                     'gambar_produk' => $result_array[0]['file_name'], //gambar produk utama
  102.                     'tgl_upload' => $tanggal,
  103.                     'meta_title' => $this->input->post('meta_title',TRUE),
  104.                     'meta_desc' => $this->input->post('meta_desc',TRUE),
  105.                     'meta_keyword' => $this->input->post('meta_keyword',TRUE),
  106.                     'status_tampil' => $this->input->post('status_tampil',TRUE),
  107.                    
  108.                     );
  109.  
  110.             $data_gambar  = array(
  111.                     'gambar_1' => $result_array[1]['file_name'], //gambar produk galeri
  112.                     'gambar_2' => $result_array[2]['file_name'], //gambar produk galeri
  113.                     'gambar_3' => $result_array[3]['file_name'], //gambar produk galeri
  114.                     'id_produk' => $kode,
  115.                      );
  116.  
  117.                 $this->Produk_model->insert2($data_gambar); // simpan ke tabel galeri produk
  118.                 $this->Produk_model->insert1($data); // simpan ke tabel produk    
  119.                 redirect(site_url('webadmin/produk'));
  120.                    
  121.         }  
  122.        
  123.     }
Advertisement
Add Comment
Please, Sign In to add comment