/** * @param \Symfony\Component\HttpFoundation\File\UploadedFile $file * @param \HorseStories\Models\Horses\Horse $horse * @param bool $profile * @return \HorseStories\Models\Pictures\Picture */ public function uploadPicture($file, $horse, $profile = false) { $extension = $file->getClientOriginalExtension(); $path = '/uploads/pictures/' . $horse->id; $fileName = str_random(12); $pathToFile = storage_path() . '/app' . $path . '/' . $fileName . '.' . $extension; $picture = new Picture(); $picture->path = $fileName . '.' . $extension; $picture->horse_id = $horse->id; $picture->mime = $file->getClientMimeType(); $picture->original_name = $file->getClientOriginalName(); $picture->profile_pic = $profile; $picture->save(); if (!file_exists(storage_path() . $path)) { $this->file->makeDirectory($path); } $this->image->make($file->getrealpath())->resize(null, 350, function ($constraints) { $constraints->aspectRatio(); })->save($pathToFile); return $picture; }
/** * @param int $horseId * @param string $path * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response */ public function getImage($horseId, $path) { $image = Picture::where('path', $path)->firstOrFail(); $file = Storage::disk()->get('/uploads/pictures/' . $horseId . '/' . $path); return response($file, 200, ['Content-Type' => $image->mime]); }