예제 #1
0
 /**
  * Store a newly created Product in database.
  *
  * @return Response
  */
 public function store(ProductRequest $request)
 {
     /**
      * Take all inputs except image, store image in seperate variable
      *
      * @var Array
      */
     $input = $request->except('image');
     $image = $request->file('image');
     if ($image != null) {
         // Picture name will be same as SKU
         $name = $input['sku'];
         // Extenstion of original picture
         $extension = '.' . $image->getClientOriginalExtension();
         // Set paths for full image and thumbnail
         $imagePath = 'img/' . $name . $extension;
         $thumbnailPath = 'img/thumbs/' . $name . $extension;
         // Save original picture
         \Image::make($image->getRealPath())->save(public_path($imagePath));
         // Save resized thumbnail
         \Image::make($image->getRealPath())->resize(300, null, function ($constraint) {
             $constraint->aspectRatio();
         })->save(public_path($thumbnailPath));
     } else {
         // Set default 'No image avaliable' images
         $imagePath = self::DEFAULT_IMG;
         $thumbnailPath = self::DEFAULT_IMG;
     }
     // Create Product model and save pictures
     $product = Product::create($input);
     $product->image = $imagePath;
     $product->image_thumb = $thumbnailPath;
     $product->save();
     return redirect(route('AdminProductIndex'));
 }