Ejemplo n.º 1
0
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(UploadImageRequest $request, $user)
 {
     if (!file_exists(public_path() . '/upload')) {
         mkdir(public_path() . '/upload');
     }
     if (!file_exists(public_path() . '/upload/images/')) {
         mkdir(public_path() . '/upload/images');
     }
     $id = Auth::user()->id;
     $auth = substr(md5(Auth::user()->id . Auth::user()->email), 0, 10);
     $resource = $request->except(['_token', 'image']);
     //dd($request->file('image'));
     if (!$request->get('album_id')) {
         $data_album = ['album_name' => 'Untitled', 'album_title' => 'Untitled album', 'user_id' => $id];
         if (!($album = Album::create($data_album))) {
             return redirect()->route('photo.create', $user)->withErrors('Unexpected error!');
         }
         $resource['album_id'] = $album->id;
     }
     if ($request->hasFile('image')) {
         $file = $request->file('image');
         $imageName = date('d-m-Y_h-i-s') . '_' . $id . '_' . $file->getClientOriginalName();
         $path = $this->path . '/' . $id . '_' . $auth;
         $destination = $path;
         $folder = public_path() . '/' . $path;
         if (!file_exists($folder)) {
             mkdir($folder);
         }
         $image = ['image_name' => $imageName, 'image_size' => $file->getSize(), 'user_id' => $id, 'fullsize_url' => $path . '/' . $imageName];
         if ($file->move($destination, $imageName) && Image::create(array_merge($resource, $image))) {
             return response()->json(['message' => 'Your photo has been upload!']);
         }
     }
     return response()->json(['message' => 'Unexpected errors']);
 }