Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function actionSimpan(){
- if (isset($_POST['simpan'])) {
- $produk_id=$_POST['produk_id'];
- $img = $_FILES['gambar'];
- error_reporting(0);
- $ukuran = 1024 * 1000; //maksimal 1mb
- $width_size = 437;
- $folder = "images/thumbs/";
- //memanggil fungsi reArrayfile lihat script atas actionSimpan
- foreach ($_POST['nama_produk'] as $i => $nama_produk) {
- if(!empty($img))
- {
- echo 'a :'.$img['size'][$i].'<br/>';
- if ($img['size'][$i] < $ukuran)
- {
- $f_type=$img['type'][$i];
- 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")
- {
- move_uploaded_file($img['tmp_name'][$i],'./images/'.$img['name'][$i]);
- $resize_image = $folder . "resize_" . uniqid(rand()) . ".jpg";
- list( $width, $height ) = getimagesize('./images/'.$img['name'][$i]);
- $k = $width / $width_size;
- $newwidth = $width / $k;
- $newheight = $height / $k;
- // fungsi untuk membuat image yang baru
- $thumb = imagecreatetruecolor($newwidth, $newheight);
- if ($f_type== "image/png" OR $f_type=="image/PNG") {
- $source = imagecreatefrompng('./images/'.$img['name'][$i]);
- }else{
- $source = imagecreatefromjpeg('./images/'.$img['name'][$i]);
- }
- // men-resize image yang baru
- imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
- // menyimpan image yang baru
- imagejpeg($thumb, $resize_image);
- imagedestroy($thumb);
- imagedestroy($source);
- //echo substr($resize_image, 14).' : '.$nama_produk.'<br/>'; //untuk memotong kalimat 'images/thumbs/'
- $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]."')";
- echo $SQL;
- $command= Yii::app()->db->createCommand($SQL);
- $n=$command->execute();
- }else if (empty($img['name'][$i])) {
- echo "";
- }else{
- echo "<script>alert('Gagal Mengupload gambar ".$img['name'][$i]."');</script>";
- }
- }else{
- echo "<script>alert('ukuran gambar terlalu besar gaes max 1Mb !'); </script>";
- // print_r($val);
- break;
- }
- }
- }
- }
- $this->redirect(array('view','id'=>$produk_id));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement