コード例 #1
0
 /**
  * Store a newly created resource in storage.
  *
  * @param StorePhotoRequest $request
  * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
  */
 public function store(StorePhotoRequest $request)
 {
     //TODO
     // Find a safe way to getClientOriginalExtension()
     //Construct file and path info
     $file = $request->file('photo');
     $ext = '.' . $file->getClientOriginalExtension();
     $filename = time() . $ext;
     $basePath = '/uploads/img/' . $filename;
     $thumbPath = '/uploads/img/thumb/' . $filename;
     $localPath = public_path() . $basePath;
     $localThumbPath = public_path() . $thumbPath;
     //DB info
     $mimeType = $file->getClientMimeType();
     $slug = Photo::generateUniqueSlug(8);
     //Save the full image and the thumb to the server
     $imageFull = Image::make($file->getRealPath())->save($localPath);
     $imageThumb = Image::make($file->getRealPath())->widen(400)->save($localThumbPath);
     //Create the DB entry
     $imageStore = Photo::create(['path' => $basePath, 'thumb_path' => $thumbPath, 'mime_type' => $mimeType, 'slug' => $slug]);
     if ($request->ajax()) {
         return response()->json($imageStore);
     } else {
         return redirect()->route('home')->with(['global-message' => 'Uploaded!', 'message-type' => 'flash-success']);
     }
 }
コード例 #2
0
 /**
  * @param StorePhotoRequest $request
  * @return \Illuminate\Http\JsonResponse
  */
 public function storeNewImages(StorePhotoRequest $request)
 {
     $productId = $request->input('productId');
     $product = Product::findOrFail($productId);
     $path = $request->file('photo');
     $product->addMedia($path)->toCollection('images');
     return response()->json(['success' => true, 'Message' => 'Your images were successfully uploaded']);
 }