编辑:
简短地说:有没有办法调用 $kernel->getRootDir();从 Twig 扩展中?或者可能来自 DI 容器?
原问题:
我尝试使用 Imagine 在服务器上缩放图像。一切正常,因为我没有尝试通过命令行触发渲染:
在这种情况下,似乎设置了错误的路径 - 我收到错误消息:
[Twig_Error_Runtime]
An exception has been thrown during the rendering of a template ("File ../web/documents/4f59ef3f76e74_test3.jpg doesn't exist") in "....:detail.html.twig" at line 72.
我正在使用我自己写的 Twig 标签:
public function thumbnail($path,$width,$maxHeight=0,$alt="",$absolute=false){
/* @var $imagine \Imagine\Gd\Imagine */
$imagine = $this->container->get('imagine');
//$box = new \Imagine\Image\Box($width, $height);
/* @var $image \Imagine\Image\ImageInterface */
$image = $imagine->open("../web/".$path);
...
我也试过这个(当我通过浏览器请求呈现模板时两者都有效)
$image = $imagine->open($path);
$path 设置为“documents/4f59ef3f76e74_test3.jpg”
“documents/”是“web”的子目录
有任何想法吗?
最佳答案
哈——找到了!这将检索 web 文件夹的绝对系统文件路径:
$webRoot = $this->container->get('kernel')->getRootDir()."/../web";
关于Symfony2 : How to set the root path in a Symfony CLI command,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9634497/