Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.35 KB | None | 0 0
  1. public function store()
  2.     {
  3.         try {
  4.             $valid = Validator::make(Request::all(), [
  5.                 'pics' => '',
  6.                 'title' => '',
  7.             ]);
  8.  
  9.             if ($valid->passes()) {
  10.                 if (request::hasFile('pics')) {
  11.                     $files = request::file('pics');
  12.                     foreach ($files as $file)
  13.                     {
  14.                         $validator2 = Validator::make(array('file' => $file), ['file' => 'image|mimes:png,gif,jpeg,jpg|max:5000']);
  15.                         if ($validator2->fails()) {
  16.                             return redirect()->back()->withInput()->withErrors($validator2->messages());
  17.                         }
  18.                     }
  19.                 }
  20.  
  21.                 $tran = DB::transaction(function() {
  22.  
  23.                     $product = new ImageGallery;
  24.                     $product->user_id = Sentry::getUser()->id;
  25.                     $product->title = Security::xss_clean(Input::get('title'));
  26.                     $product->description = Security::xss_clean(Input::get('description'));
  27.                     $product->created_at = jDate::forge()->time();
  28.  
  29.                     $insertedProduct = $product->id;
  30.                     if (request::hasFile('pics')) {
  31.                         $files = request::file('pics');
  32.                         foreach ($files as $file) {
  33.                             $destinationPath = 'uploads/gallery';
  34.                             $filename = $file->getClientOriginalName();
  35.                             $extension = $file->getClientOriginalExtension();
  36.                             $filename = sha1($filename) . '-' . rand(1000, 9999) . '.' . $extension;
  37.                             $upload_success = $file->move($destinationPath, $filename);
  38.                             if ($upload_success)
  39.                             {
  40.                                 $img = Image::make($destinationPath . '/' . $filename);
  41.                                 //$watermark = Image::make('logo.png');
  42.                                 //$img->insert($watermark, 'bottom-right',10,10);
  43.                                 $path=$destinationPath.'/' . $filename;
  44.                                 $img->save($path);
  45.                                 $img->resize(300, null, function ($constraint) {
  46.                                     $constraint->aspectRatio();
  47.                                 });
  48.                                 //  $img->insert($watermark, 'bottom-right',10,10);
  49.                                 $img->save($destinationPath . '/tumb/' . $filename);
  50.  
  51.                                 $product->image = $filename;
  52.                                 $product->save();
  53.                             }
  54.                             else
  55.                             {
  56.                                 return redirect()->back()->with('danger', 'خطا در آپلود فایل.');
  57.                             }
  58.                         }
  59.                     }
  60.                 });
  61.                 return Redirect::route('dashboard.image-gallery.index')->with('success', 'با موفقیت ثبت شد.');
  62.             }
  63.             else
  64.             {
  65.                 return Redirect::back()->withInput()->withErrors($valid->messages());
  66.             }
  67.         } catch (LoginRequiredException $ex) {
  68.             return Redirect::back()->withInput()->with('danger',$e->getMessage());
  69.         }
  70.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement