/**
* {@inheritdoc}
*/
public function returnImage($id, $formatKey)
{
$setExpireHeaders = false;
try {
$media = $this->mediaRepository->findMediaByIdForRendering($id, $formatKey);
if (!$media) {
throw new ImageProxyMediaNotFoundException('Media was not found');
}
$fileVersion = $this->getLatestFileVersion($media);
if (!$this->checkMimeTypeSupported($fileVersion->getMimeType())) {
throw new InvalidMimeTypeForPreviewException($fileVersion->getMimeType());
}
// Convert Media to format.
$responseContent = $this->converter->convert($fileVersion, $formatKey);
// HTTP Headers
$status = 200;
$setExpireHeaders = true;
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mimeType = $finfo->buffer($responseContent);
// Save image.
if ($this->saveImage) {
$this->formatCache->save($responseContent, $media->getId(), $this->replaceExtension($fileVersion->getName(), $mimeType), $fileVersion->getStorageOptions(), $formatKey);
}
} catch (MediaException $e) {
$responseContent = null;
$status = 404;
$mimeType = null;
}
// Set header.
$headers = $this->getResponseHeaders($mimeType, $setExpireHeaders);
// Return image.
return new Response($responseContent, $status, $headers);
}