douying7289 2015-02-21 14:50
浏览 33

Symfony Gaufrette - 从文件系统下载或读取图像并以树枝形式显示

my question is simple. I use Symfony framework and Gaufrette filesystem (KnpGaufretteBundle) and I allready saved files (images) in filesystem.

Now I want show that images in twig. In controller I get the file:

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

In variable $image is Gaufrette\File object. I would like to display that image in twig in <img> tag. But i don't know how to do that. Thank you for help

  • 写回答

1条回答 默认 最新

  • dongya767979565 2015-04-01 13:56
    关注

    Gaufrette is a file system abstraction library, seems like it doesn't implement any routing options for the files.

    First option is creating a public route for your Gaufrette file system folder and reach the files as @Broncha mentioned.

    The other option is; You can use Base64 encode to show images in the template, without passing a url for the image.

    For 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())));
    

    This is the simple template file;

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料