Finalise la mise en cache des images

Fix #9
This commit is contained in:
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;
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));