Advertisement
zulmi

simpan part03

Nov 21st, 2017
1,137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.47 KB | None | 0 0
  1. public function actionSimpan(){
  2.         if (isset($_POST['simpan'])) {
  3.         $produk_id=$_POST['produk_id'];
  4.  
  5.             $img = $_FILES['gambar'];
  6.             error_reporting(0);
  7.             $ukuran =  1024 * 1000; //maksimal 1mb
  8.             $width_size = 437;
  9.             $folder = "images/thumbs/";
  10.             //memanggil fungsi reArrayfile lihat script atas actionSimpan
  11.  
  12.         foreach ($_POST['nama_produk'] as $i => $nama_produk) {
  13.         if(!empty($img))
  14.         {
  15.           echo 'a :'.$img['size'][$i].'<br/>';
  16.           if ($img['size'][$i] < $ukuran)
  17.           {
  18.             $f_type=$img['type'][$i];
  19.                         if ($f_type== "image/gif" OR $f_type== "image/png" OR $f_type== "image/jpeg" OR $f_type== "image/JPEG" OR $f_type== "image/PNG" OR $f_type== "image/GIF")
  20.                         {
  21.                             move_uploaded_file($img['tmp_name'][$i],'./images/'.$img['name'][$i]);
  22.                             $resize_image = $folder . "resize_" . uniqid(rand()) . ".jpg";
  23.  
  24.                             list( $width, $height ) = getimagesize('./images/'.$img['name'][$i]);
  25.                             $k = $width / $width_size;
  26.                             $newwidth = $width / $k;
  27.                             $newheight = $height / $k;
  28.                             // fungsi untuk membuat image yang baru
  29.                             $thumb = imagecreatetruecolor($newwidth, $newheight);
  30.                             if ($f_type== "image/png" OR $f_type=="image/PNG") {
  31.                             $source = imagecreatefrompng('./images/'.$img['name'][$i]);
  32.                             }else{
  33.                             $source = imagecreatefromjpeg('./images/'.$img['name'][$i]);
  34.                             }
  35.                             // men-resize image yang baru
  36.                             imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  37.                             // menyimpan image yang baru
  38.                             imagejpeg($thumb, $resize_image);
  39.                             imagedestroy($thumb);
  40.                             imagedestroy($source);
  41.  
  42.                             //echo substr($resize_image, 14).' : '.$nama_produk.'<br/>'; //untuk memotong kalimat 'images/thumbs/'
  43.  
  44.                             $SQL="INSERT INTO bp_produk values('','$nama_produk','".$_POST['stok'][$i]."','".$_POST['ukuran'][$i]."','".substr($resize_image, 14)."','$produk_id','".$_POST['jenis'][$i]."','".$_POST['models'][$i]."')";
  45.                             echo $SQL;
  46.                             $command= Yii::app()->db->createCommand($SQL);
  47.                             $n=$command->execute();
  48.                        
  49.                         }else if (empty($img['name'][$i])) {
  50.                             echo "";
  51.                         }else{
  52.                             echo "<script>alert('Gagal Mengupload gambar ".$img['name'][$i]."');</script>";
  53.                         }  
  54.                     }else{
  55.                         echo "<script>alert('ukuran gambar terlalu besar gaes max 1Mb !'); </script>";
  56.                         // print_r($val);
  57.                         break;
  58.                     }
  59.                 }  
  60.             }
  61.        
  62.         }
  63.         $this->redirect(array('view','id'=>$produk_id));
  64.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement