gpt4 book ai didi

php - Symfony Gaufrette - 从文件系统下载或读取图像并将其显示在 Twig 中

转载 作者:行者123 更新时间:2023-12-02 05:17:29 26 4
gpt4 key购买 nike

我的问题很简单。我使用 Symfony 框架和 Gaufrette 文件系统 (KnpGaufretteBundle),并且我已经在文件系统中保存了文件(图像)。

现在我想在 Twig 中显示图像。在 Controller 中我得到了文件:

$image = $filesystem->get($fie_key);

在变量 $image 中是 Gaufrette\File 对象。我想在 <img> 中的 Twig 中显示该图像标签。但我不知道该怎么做。谢谢你的帮助

最佳答案

Gaufrette 是一个文件系统抽象库,似乎没有为文件实现任何路由选项。

第一个选项是为您的 Gaufrette 文件系统文件夹创建一个公共(public)路径,并访问@Broncha 提到的文件。

另一种选择是;您可以使用 Base64 编码在模板中显示图像,而无需传递图像的 url。

对于e.q

<?php
require_once('vendor/autoload.php');

use Gaufrette\Filesystem;
use Gaufrette\File;
use Gaufrette\Adapter\Local as LocalAdapter;

$adapter = new LocalAdapter('files');
$filesystem = new Filesystem($adapter);

$loader = new Twig_Loader_Filesystem('templates');
$twig = new Twig_Environment($loader, array(
'cache' => 'tmp',
));
//Probably the top part is redundant for you
//I've just set up a local twig and Gaufrette environment.

$file_key = "shareimage.png";
$file = $filesystem->get($file_key);
echo $twig->render('index.html', array('imageContent' => "data: ".$file->getMtime().";base64,".base64_encode($file->getContent())));

这是简单的模板文件;

<html>
<body>
<img src="{{imageContent}}" alt="Image" />
</body>
</html>

关于php - Symfony Gaufrette - 从文件系统下载或读取图像并将其显示在 Twig 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28647102/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com