我正在尝试使用PHP
和twig
来显示数据库中的图像。实现这一目标我遇到了很大麻烦。在我的index.twig
我有这行代码:<a><img class="images" src="showImg.php?id={{ text.getImage() }}" /></a>
。
{{ text.getImage() }}
来自for-loop
而showImg.php
将根据给定的ID
参数进行选择图片的查询。
在我的showImg.php
:
if (isset($_GET['id'])) {
$id = intval($_GET['id']);
$sql = "SELECT filename, mime, code FROM Img_col WHERE id = :id";
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
while($row = $stmt->fetchObject('Image')) {
$this->result[] = $row;
}
Header("Content-type: $img->getMime()");
Header("Content-Disposition: filename=\"$img->getFileName()\"");
$img->hentCode();
}
我的第一个问题是IDE停在第$stmt = $this->db->prepare($sql);
行,第二个问题是如何将图片(code
)发送到index.twig
。
任何想法或暗示帮助我都非常感激。我愿意接受任何建议
答案 0 :(得分:0)
我不确定你想要实现什么,但这可以在Symfony中快速完成。您需要一个控制器,它将根据路线创建您的响应:
/**
* @Route("/image/{id}", name="image")
*/
public function imageAction($id)
{
$image = $this->getDoctrine()->getRepository('AppBundle:Image')->findOneBy(array('id'=> $id));
return $this->render('image.html.twig', array('image' => $image));
}
显然,你的AppBundle中需要一个简单的Image类,至少有两个字段:id和imageName。
在你的image.html.twig中:
<img src="/images/{{ image.imageName }}"/>
查看文档以获取更多信息: Symfony Documentation