waner
waner

Reputation: 67

symfony 3 - url image from database

I wanted to display the url of an image from bdd in my file twig I receive an error of the image like the: bdd

this is my bdd : bdd

and my view from twig :

{% extends '@App/layout.html.twig' %}

{% block title %} Illustration {% endblock %}

{% block body %}
<div class="col-md-4 col-md-offset-4 text-center">
    <h2> {{ imageFormation.image.nom }}</h2>
    <hr>
    <img src=" {{ imageFormation.image.url |raw }} "/><br><br>
</div>
{% endblock %}

Can you help me solve my problem please? i dont know why i cant get the real image.

edit :

/**
     * @Route("/formation/{formation}" , name="image")
     * @param $image
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function imageFormationAction($formation){


        $doctrine = $this->container->get('doctrine');
        $em = $doctrine->getManager();

        $imageRepository = $em->getRepository('AppBundle:Formation');

        $imageFormation = $imageRepository->findOneByid($formation);

        return $this->render('@App/formation/formationImage.html.twig', array(
            'imageFormation' => $imageFormation
        ));
    }

bdd

bdd

Upvotes: 1

Views: 2099

Answers (2)

Zain Saqer
Zain Saqer

Reputation: 304

Your image url must be relative to web directory, like this fichier\Mention.jpg, not the image's path in your file system C:\xampp\htdocs\Symfony\MonProjetCv\web\fichier\Mention.jpg.

Save a relative path to your image file in the database (url equal fichier\Mention.jpg), and use asset function in your twig file:

{% extends '@App/layout.html.twig' %}

{% block title %} Illustration {% endblock %}

{% block body %}
<div class="col-md-4 col-md-offset-4 text-center">
    <h2> {{ imageFormation.image.nom }}</h2>
    <hr>
    <img src=" {{ asset(imageFormation.image.url); }} "/><br><br>
</div>
{% endblock %}

Upvotes: 1

Nirjhor
Nirjhor

Reputation: 73

Can you please try this way?

<img src='{{ asset("IMAGE_DIRECTORY_PATH/"~ image.url)}}' />

Upvotes: 0

Related Questions