Finalise la mise en cache des images

Fix #9
This commit is contained in:
Clement Desmidt 2020-03-12 17:31:51 +01:00
parent 020eec2a60
commit 7f1214b96b
4 changed files with 91 additions and 29 deletions

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Post; use App\Post;
use App\Services\ImageService;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\File;
use Intervention\Image\Constraint; use Intervention\Image\Constraint;
@ -16,10 +17,11 @@ class ImageController extends Controller
* @param int $post_id * @param int $post_id
* @param string|array $options * @param string|array $options
* @param string $image * @param string $image
* @param ImageService $image_service
* *
* @return mixed * @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()) { if (Auth::guest()) {
throw new UnauthorizedHttpException('Vous devez être connecté pour voir cette image.'); 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); $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 ('o:full' === $options) {
if (!is_readable($original)) { $img = $image_service->makeThumbnail($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)));
} else { } else {
$img = Image::make($thumb); $img = $image_service->makeThumbnail($original, ['width' => 300]);
} }
return $img->response(File::extension($original)); return $img->response(File::extension($original));

View File

@ -0,0 +1,29 @@
<?php
namespace App\Providers;
use App\Services\ImageService;
use Illuminate\Support\ServiceProvider;
class ImageServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->app->bind(ImageService::class);
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
}

View File

@ -0,0 +1,56 @@
<?php
namespace App\Services;
use Illuminate\Support\Facades\Auth;
use Intervention\Image\Constraint;
use Intervention\Image\Facades\Image;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ImageService
{
/**
* @param string $image path to image
* @param array $options option to make thumbnail
*
* @return \Intervention\Image\Image
*/
public function makeThumbnail($image, $options = [])
{
if (!is_readable($image)) {
throw new NotFoundHttpException();
}
$img = Image::make($image);
if (empty($options)) {
return $img;
}
$folder = dirname($img->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;
}
}

View File

@ -176,6 +176,7 @@ return [
App\Providers\RouteServiceProvider::class, App\Providers\RouteServiceProvider::class,
\App\Providers\TagDetectorProvider::class, \App\Providers\TagDetectorProvider::class,
\App\Providers\ImageServiceProvider::class,
Intervention\Image\ImageServiceProvider::class, Intervention\Image\ImageServiceProvider::class,
], ],