public function thumbAction(Request $request, Application $app)
{
$source = $request->get('src', false);
$width = $request->get('width', 250);
// Do requested thumbnail in correct format already exists ?
if ($app['flysystems']['thumbs']->has($width . "/" . $source)) {
return $app->redirect($request->getBasePath() . '/thumbs/' . $width . '/' . $source, 301);
}
// Do requested file exists ?
if (!$source || !$app['flysystems']['local']->has($source)) {
return new Response("Source file not found.", 404);
}
try {
$contents = $app['flysystems']['local']->read($source);
$imageManager = new ImageManager();
$image = $imageManager->make($contents);
$image->resize($width, null, function ($constraint) {
$constraint->aspectRatio();
});
$info = $app['flysystems']['local']->getWithMetadata($source, ['mimetype']);
$image->encode($info['mimetype']);
$app['flysystems']['thumbs']->put($width . "/" . $source, $image);
return $app->redirect($request->getBasePath() . '/thumbs/' . $width . '/' . $source, 301);
} catch (\Exception $e) {
return new Response("Erreur !", 500);
}
// Should not happen, everything failed. Display not found image :(
return $app->redirect($request->getBasePath() . '/assets/img/' . $width . '_not-found.png', 302);
}