<?php
namespace Nen\Bundle\KennisbankPlatformBundle\Controller;
use League\Glide\Server;
use League\Glide\Signatures\SignatureException;
use League\Glide\Signatures\SignatureInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Intervention\Image\Exception\NotReadableException;
use League\Flysystem\FilesystemInterface;
use Symfony\Component\HttpFoundation\StreamedResponse;
class ImagesController extends AbstractController
{
/**
* @Route("/image/{path}", name="images_server", requirements={"path"=".+"})
*
* @param string $path
* @param Request $request
* @param SignatureInterface $signature
* @param Server $server
* @return Response
*/
public function server(string $path, Request $request, SignatureInterface $signature, Server $server, FilesystemInterface $filesystem)
{
$options = $request->query->all();
try {
$signature->validateRequest($path, $options);
} catch (SignatureException $e) {
throw $this->createNotFoundException();
}
// @todo(guido): needs to be a different method to retrieve the prefix.
if (strpos($path, 'publisher/') === 0) {
$path = 'publisher://'.$path;
} elseif (strpos($path, 'uploads/') === 0) {
$path = substr($path, 8);
$path = 'uploads://'.$path;
} else {
$path = 'cms://' . $path;
}
try {
return $server->getImageResponse($path, $options);
} catch (NotReadableException $e) {
$stream = $filesystem->readStream($path);
$response = new StreamedResponse(
function() use ($stream) {
$outputStream = fopen('php://output', 'wb');
$fileStream = $stream;
stream_copy_to_stream($fileStream, $outputStream);
});
$detector = new \League\MimeTypeDetection\FinfoMimeTypeDetector();
$mimeType = $detector->detectMimeTypeFromPath($path);
$response->headers->set('Content-Type', $mimeType);
return $response;
}
}
}