From 7f1214b96b39d2e74230eaca0142a97cf42e79af Mon Sep 17 00:00:00 2001 From: Clement Desmidt Date: Thu, 12 Mar 2020 17:31:51 +0100 Subject: [PATCH] :sparkles: Finalise la mise en cache des images Fix #9 --- app/Http/Controllers/ImageController.php | 34 +++----------- app/Providers/ImageServiceProvider.php | 29 ++++++++++++ app/Services/ImageService.php | 56 ++++++++++++++++++++++++ config/app.php | 1 + 4 files changed, 91 insertions(+), 29 deletions(-) create mode 100644 app/Providers/ImageServiceProvider.php create mode 100644 app/Services/ImageService.php diff --git a/app/Http/Controllers/ImageController.php b/app/Http/Controllers/ImageController.php index 8b8a49c..1d97c85 100644 --- a/app/Http/Controllers/ImageController.php +++ b/app/Http/Controllers/ImageController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Post; +use App\Services\ImageService; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\File; use Intervention\Image\Constraint; @@ -16,10 +17,11 @@ class ImageController extends Controller * @param int $post_id * @param string|array $options * @param string $image + * @param ImageService $image_service * * @return mixed */ - public function display($post_id, $options = 'o:full', $image) + public function display($post_id, $options = 'o:full', $image, ImageService $image_service) { if (Auth::guest()) { throw new UnauthorizedHttpException('Vous devez ĂȘtre connectĂ© pour voir cette image.'); @@ -32,36 +34,10 @@ class ImageController extends Controller } $original = sprintf('%s/%s/%s', storage_path('app'), Auth::user()->getFolder(), $image); - $thumb = sprintf('%s/%s/300-%s', storage_path('app'), Auth::user()->getFolder(), $image); - $img = Image::make($original); if ('o:full' === $options) { - if (!is_readable($original)) { - throw new NotFoundHttpException(); - } - } elseif (!is_readable($thumb)) { - if (!is_readable($original)) { - throw new NotFoundHttpException(); - } - $options = explode('!', $options); - $width = null; - $height = null; - foreach ($options as $option) { - [$current_option_name, $current_option_value] = explode(':', $option); - switch ($current_option_name) { - case 'w': - $width = $current_option_value; - break; - case 'h': - $height = $current_option_value; - break; - } - } - //http://image.intervention.io/getting_started/ - $img->resize($width, $height, static function (Constraint $constraint) { - $constraint->aspectRatio(); - })->save(sprintf('%s/%s/%s', storage_path('app'), Auth::user()->getFolder(), sprintf('300-%s', $image))); + $img = $image_service->makeThumbnail($original); } else { - $img = Image::make($thumb); + $img = $image_service->makeThumbnail($original, ['width' => 300]); } return $img->response(File::extension($original)); diff --git a/app/Providers/ImageServiceProvider.php b/app/Providers/ImageServiceProvider.php new file mode 100644 index 0000000..2034dff --- /dev/null +++ b/app/Providers/ImageServiceProvider.php @@ -0,0 +1,29 @@ +app->bind(ImageService::class); + } + + /** + * Bootstrap services. + * + * @return void + */ + public function boot() + { + // + } +} diff --git a/app/Services/ImageService.php b/app/Services/ImageService.php new file mode 100644 index 0000000..2e95f20 --- /dev/null +++ b/app/Services/ImageService.php @@ -0,0 +1,56 @@ +basePath()); + $file_name = basename($image); + + $width = null; + if (array_key_exists('width', $options)) { + $width = $options['width']; + $file_name = sprintf('w%u-%s', $width, $file_name); + } + + $height = null; + if (array_key_exists('$height', $options)) { + $height = $options['$height']; + $file_name = sprintf('h%u-%s', $height, $file_name); + } + + $full_path = sprintf('%s/%s', $folder, $file_name); + if (!array_key_exists('force', $options) && is_readable($full_path)) { + return Image::make($full_path); + } + + //http://image.intervention.io/getting_started/ + $img->resize($width, $height, static function (Constraint $constraint) { + $constraint->aspectRatio(); + })->save(sprintf('%s/%s', $folder, $file_name)); + + return $img; + } +} diff --git a/config/app.php b/config/app.php index 54da053..1b675c2 100644 --- a/config/app.php +++ b/config/app.php @@ -176,6 +176,7 @@ return [ App\Providers\RouteServiceProvider::class, \App\Providers\TagDetectorProvider::class, + \App\Providers\ImageServiceProvider::class, Intervention\Image\ImageServiceProvider::class, ],