/** * {@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); }
/** * {@inheritdoc} */ public function returnImage($id, $formatName) { $setExpireHeaders = false; try { // load Media $media = $this->mediaRepository->findMediaById($id); if (!$media) { throw new ImageProxyMediaNotFoundException('Media was not found'); } // load Media Data list($fileName, $version, $storageOptions, $mimeType) = $this->getMediaData($media); try { // check if file has supported preview if (!$this->checkPreviewSupported($mimeType)) { throw new InvalidMimeTypeForPreviewException($mimeType); } // get format options $format = $this->getFormat($formatName); $formatOptions = $format['options']; // load Original $uri = $this->originalStorage->load($fileName, $version, $storageOptions); $original = $this->createTmpFile($this->getFile($uri, $mimeType)); // prepare Media $this->prepareMedia($mimeType, $original); // convert Media to format $image = $this->converter->convert($original, $format); // remove profiles and comments $image->strip(); // set Interlacing to plane for smaller image size if (count($image->layers()) == 1) { $image->interlace(ImageInterface::INTERLACE_PLANE); } // set extension $imageExtension = $this->getImageExtension($fileName); // get image $responseContent = $image->get($imageExtension, $this->getOptionsFromImage($image, $imageExtension, $formatOptions)); // HTTP Headers $status = 200; $setExpireHeaders = true; // save image if ($this->saveImage) { $this->formatCache->save($this->createTmpFile($responseContent), $media->getId(), $this->replaceExtension($fileName, $imageExtension), $storageOptions, $formatName); } } catch (MediaException $e) { // return when available a file extension icon list($responseContent, $status, $imageExtension) = $this->returnFileExtensionIcon($formatName, $this->getRealFileExtension($fileName), $e); } $responseMimeType = 'image/' . $imageExtension; } catch (MediaException $e) { $responseContent = $e->getCode() . ': ' . $e->getMessage(); $status = 404; $responseMimeType = 'text/plain'; } // clear temp files $this->clearTempFiles(); // set header $headers = $this->getResponseHeaders($responseMimeType, $setExpireHeaders); // return image return new Response($responseContent, $status, $headers); }