jcarloslb

Upload de imagens gerando thumbnail, salvando no db

Nov 10th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.68 KB | None | 0 0
  1. <?php
  2.  
  3.     function inserir(){
  4.  
  5.        
  6.         $id_galeria = $this->input->post('id_galeria');
  7.        
  8.         $query = $this->db->query("SELECT * FROM galerias WHERE id='$id_galeria'");
  9.        
  10.         foreach($query->result() as $row){
  11.             $pasta = $row->pasta;
  12.         }
  13.        
  14.         $upload_conf = array(
  15.             'upload_path' => realpath('uploads/galerias').'/'.$pasta.'/',
  16.             'allowed_types' => 'gif|jpg|png|jpeg',
  17.             'max_size' => '30000',
  18.             'file_name' => md5(uniqid(time())) . ".jpg"
  19.         );
  20.  
  21.         $this -> upload -> initialize($upload_conf);
  22.  
  23.         // Change $_FILES to new vars and loop them
  24.         foreach ($_FILES['userfile'] as $key => $val) {
  25.             $i = 1;
  26.             foreach ($val as $v) {
  27.                 $field_name = "file_" . $i;
  28.                 $_FILES[$field_name][$key] = $v;
  29.                 $i++;
  30.             }
  31.         }
  32.        
  33.         // Unset the useless one ;)
  34.         unset($_FILES['userfile']);
  35.  
  36.         // Put each errors and upload data to an array
  37.         $error = array();
  38.         $success = array();
  39.  
  40.         // main action to upload each file
  41.         foreach ($_FILES as $field_name => $file) {
  42.            
  43.             if (!$this -> upload -> do_upload($field_name)) {
  44.                 // if upload fail, grab error
  45.                 //$error['upload'][] = $this -> upload -> display_errors();
  46.                 echo '<script>alert("Não foi possível cadastrar as imagens, tente mais tarde.");</script>';
  47.                 echo '<script>location.href="'.base_url('adm_galerias_fotos').'";</script>';
  48.                 return false;
  49.                
  50.             } else {
  51.                 // otherwise, put the upload datas here.
  52.                 // if you want to use database, put insert query in this loop
  53.                 $upload_data = $this -> upload -> data();
  54.                
  55.                 // set the resize config
  56.                 $resize_conf = array(
  57.                 // it's something like "/full/path/to/the/image.jpg" maybe
  58.                 'source_image' => $upload_data['full_path'],
  59.                 // and it's "/full/path/to/the/" + "thumb_" + "image.jpg
  60.                 // or you can use 'create_thumbs' => true option instead
  61.                 //'new_image' => $upload_data['file_path'] . 'thumb_' . $upload_data['file_name'],
  62.                 'new_image' => $upload_data['file_path']. 'thumbs/' . $upload_data['file_name'],
  63.                 'width' => 300,
  64.                 'height' => 178);
  65.  
  66.                 // initializing
  67.                 $this -> image_lib -> initialize($resize_conf);
  68.                
  69.                 // do it!
  70.                 if (!$this -> image_lib -> resize()) {
  71.                     // if got fail.
  72.                     $error['resize'][] = $this -> image_lib -> display_errors();
  73.                 } else {
  74.                     // otherwise, put each upload data to an array.
  75.                     $success[] = $upload_data;
  76.                 }
  77.                
  78.                 $img_db = $upload_data['file_name'];
  79.                
  80.                 $_POST['foto'] = $img_db;
  81.                
  82.                 $this->fotos_model->insert();
  83.                
  84.             }
  85.         }
  86.  
  87.         // see what we get
  88.         if (count($error > 0)) {
  89.             $data['error'] = $error;
  90.         } else {
  91.             $data['success'] = $upload_data;
  92.         }
  93.  
  94.         echo '<script>alert("Fotos cadastradas com sucesso");</script>';
  95.     }
Advertisement
Add Comment
Please, Sign In to add comment