【问题标题】:symfony 3 - url image from databasesymfony 3 - 来自数据库的 url 图像
【发布时间】:2017-10-22 13:14:25
【问题描述】:

我想在我的文件 twig 中显示来自 bdd 的图像的 url 我收到如下图像错误:

这是我的 bdd :

和我从树枝上的观点:

{% 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 %}

你能帮我解决我的问题吗? 我不知道为什么我不能得到真实的图像。

编辑:

/**
     * @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
        ));
    }

【问题讨论】:

  • 为什么你的图片需要绝对路径?使用相对路径可以轻松完成这项工作。只需将图片放在web/directory1 中,然后使用&lt;img src="{{ asset('directory1/' ~ imageFormation.image.url }}"&gt;

标签: php database image symfony


【解决方案1】:

您的图片 url 必须是相对于 web 目录的,例如 fichier\Mention.jpg,而不是图片在文件系统中的路径 C:\xampp\htdocs\Symfony\MonProjetCv\web\fichier\Mention.jpg

在数据库中保存图像文件的相对路径(url 等于fichier\Mention.jpg),并在树枝文件中使用asset 函数:

{% 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 %}

【讨论】:

  • ohhhh 非常感谢你,现在它很好。我知道这是 url 的问题,但我没有尝试你的方法。 “路径”不适合这个功能,“资产”是好的,现在我有了我想要的:)。非常感谢
  • 我误写了path 而不是assetasset 是你想要的,path 用来指路由。我会更正答案。
【解决方案2】:

你可以试试这个方法吗?

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

【讨论】:

  • 我这样做了: 和它仍然是同一件事
  • 请尝试:
  • 我试过了,但它的风格一样,我不知道问题是否来自 ma 数据库。我需要数据库中的图像,因为我需要从页面的 URL 进行循环。我将编辑我的问题,看看我需要什么
猜你喜欢
  • 2021-01-15
  • 1970-01-01
  • 2017-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-08
  • 1970-01-01
相关资源
最近更新 更多