0

I need to display images that are stored outside of the project directory. Here's the structure :

  • symfony project
  • second project/
    • /src/
    • /assets/
    • /images

Giving the direct path did not work. Like so : <img src="../../../second_project/src/assets/images/filename">, using the {{asset ('direct_path') didn't do the job either.

I'm currently trying to use a controller path to do so, it currently looks like this :

     /**
     * @Route("/admin/getphoto/{filename}", name="get_photo")
     */
    public function getPhoto($filename) {
        $path = realpath($this->getParameter('uploads_directory' . '/' . $filename));
        return file($path, ['ContentType' => 'image/jpeg']);
    }

with my template looking like this :

<img src="{{ path('get_photo', {'filename' : photo.tag} ) }}" alt="">

yivi
  • 42,438
  • 18
  • 116
  • 138
LosMomos
  • 21
  • 3

1 Answers1

1
     /**
     * @Route("/admin/getphoto/{filename}", name="get_photo")
     */
    public function getPhoto($filename) {
        $file = $this->getParameter('uploads_directory') . '/' . $filename;
        $response = new Response();
        $disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE, $filename);
        $response->headers->set('Content-Disposition', $disposition);
        $response->headers->set('Content-Type', 'image/png');
        $response->setContent(file_get_contents($file));

        return $response;
    }

with

<img src="{{ path('get_photo', {'filename' : photo.tag} ) }}" alt="">

worked wonders, thanks !

LosMomos
  • 21
  • 3