2012-05-04 63 views
2

当我的图像保存在我的包目录结构显示图像很容易与Assetic从文档:Symfony2 Assetic:如何显示上传图像的路径存储在数据库中?

{% image '@AcmeFooBundle/Resources/public/images/example.jpg' 
    filter='jpegoptim' output='/images/example.jpg' 
%} 
<img src="{{ asset_url }}" alt="Example"/> 
{% endimage %} 

,但我该如何使用assetic我上传的图片?在我的getWebPath下面返回相对于web dir的路径,例如:“/uploads/images/image.jpg”。也许这个解决方案是好的,不需要使用资产?我觉得用它像上面jpegoptim一些过滤可能是有用的

<img src="{{app.request.basepath}}/{{ entity.getWebPath }}"> 

回答

5
<img src="{{ app.request.scheme ~ '://' ~ app.request.host ~ '/' ~ entity.getWebPath }}" /> 
+0

我在第二段代码中完成了同样的工作,问题是如何使用Assetic做到这一点。 – nysander

+0

在你的getWebPath返回相对于web dir的路径例如:“/uploads/images/image.jpg”你只能做到这一点,它完美的工作'',浏览器会自动添加主机名:) –

+0

我试过了然后路径相对于我的行为和参数路由f.ex:app.php/action/param1/param2/uploads/images/image.jpg,结果得到了404图像 – nysander

0

,你可以把它们存储在该automaticcaly生成的Web direectory '上传/文件/' 使用getUploadRootDir(),getAbsolutePath().. 。并打电话给他们使用
src =“{{asset('uploads/files /'〜entity.path)}}”

相关问题