使用 png 优化的静态资源转储引用了错误的文件
我正在尝试使用 Symfony2 构建一个项目,并包含所有可能的优化。其中之一是对 html 中使用的图像进行优化。 Symfony2 有一个捆绑包 Assetic,允许使用例如来自 Twig 模板的 optipng 来实现此目的。文档在这里: http://symfony.com/doc/2.0/cookbook/ assetic/jpeg_optimize.html
我遇到的问题是,一切都在开发环境中运行(所有资产资产都通过控制器提供),但 CLI 命令转储到一个文件,未在渲染模板中使用。
这是转储资源时的输出,文件随后在 /web/assetic 中可用,
Dumping all prod assets.
Debug mode is off.
[file+] /home/projects/dashboard/data/dashboard/app/../web/js/4a3b4dc.js
[file+] /home/projects/dashboard/data/dashboard/app/../web/css/9640074.css
[file+] /home/projects/dashboard/data/dashboard/app/../web/assetic/1d666d2.png
[file+] /home/projects/dashboard/data/dashboard/app/../web/assetic/dfaa6c9.png
[file+] /home/projects/dashboard/data/dashboard/app/../web/assetic/5f2dd31.png
当我查看页面时,这些 url 被称为
<img src="/assetic/ad39e3f.png">
<img src="/assetic/69fbd4a.png">
<img src="/assetic/e4a4ede.png">
Css 和 js,但确实有效。
我的配置文件:
# Assetic Configuration
assetic:
debug: %kernel.debug%
use_controller: false
java: /usr/bin/java
filters:
cssrewrite: ~
closure:
jar: %kernel.root_dir%/Resources/java/compiler.jar
yui_css:
jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar
optipng:
apply_to: "\.png$"
level: 3
jpegoptim:
apply_to: "\.jpe?g$"
strip_all: true
twig:
functions:
jpegoptim: { output: images/*.jpg }
optipng: { output: images/*.png }
我的树枝模板:
<img src="{{ optipng('@KunstmaanDashboardBundle/Resources/public/images/foursquare-logo.png') }}" style="margin-bottom: 0;" />
I'm trying to build a project using Symfony2 and all possible optimizations included. One of such is the optimizing of images used in the html. Symfony2 has a bundle Assetic that allows for this using e.g. optipng right from the Twig templates. The docs are here: http://symfony.com/doc/2.0/cookbook/assetic/jpeg_optimize.html
The problem i'm having is that everything works in the dev environment (where all assetic assets are served through a controller) but that the CLI command dumps to a file, not used in the rendered templates.
This is the output when dumping the assets, the files are available afterwards in /web/assetic
Dumping all prod assets.
Debug mode is off.
[file+] /home/projects/dashboard/data/dashboard/app/../web/js/4a3b4dc.js
[file+] /home/projects/dashboard/data/dashboard/app/../web/css/9640074.css
[file+] /home/projects/dashboard/data/dashboard/app/../web/assetic/1d666d2.png
[file+] /home/projects/dashboard/data/dashboard/app/../web/assetic/dfaa6c9.png
[file+] /home/projects/dashboard/data/dashboard/app/../web/assetic/5f2dd31.png
When i view the page these url's are called
<img src="/assetic/ad39e3f.png">
<img src="/assetic/69fbd4a.png">
<img src="/assetic/e4a4ede.png">
Css and js however do work.
My config file:
# Assetic Configuration
assetic:
debug: %kernel.debug%
use_controller: false
java: /usr/bin/java
filters:
cssrewrite: ~
closure:
jar: %kernel.root_dir%/Resources/java/compiler.jar
yui_css:
jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar
optipng:
apply_to: "\.png$"
level: 3
jpegoptim:
apply_to: "\.jpe?g$"
strip_all: true
twig:
functions:
jpegoptim: { output: images/*.jpg }
optipng: { output: images/*.png }
My twig template:
<img src="{{ optipng('@KunstmaanDashboardBundle/Resources/public/images/foursquare-logo.png') }}" style="margin-bottom: 0;" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我解决了这个问题,用两种可能的树枝语法进行了一些测试。
我从以下配置开始,
在每次更改之间运行以下命令,以确保缓存清晰并且正在转储新文件。
00 首先转储输出
并生成 html
注意:
write_to
参数,但使用在framework.templatating.assets_base_urls.http
中指定的文件夹后面的默认文件夹,01 在 config.yml 中指定输出目录
注意: 您将得到完全相同的结果。这两种语法都不处理配置中的“输出”(仅在树枝中)。
02 在 twig 模板中指定输出
顺便说一句,我将
optimasset/
添加到输出路径,因为渲染不考虑````write_to```价值。dump:
render as:
注意: 渲染正确,但转储路径不正确。为了解决这个问题,我必须删除
write_to
参数。结论
{{ optipng(...) }}
因为{{ jpegoptim(...) }}
永远不会输出好的结果文件名。使用语法
{% images ... %}
。write_to
{% images %} 在渲染 html 时不考虑它,仅使用{% javascripts %}
和 < code>{% stylesheets %} 即可。I worked around that problem doing some tests with the two possible twig syntaxes.
I started with the following configuration
between each change I ran the following commands to make sure the cache was clear and new files were dumping.
00 First the dump output was
and generated html
Note:
write_to
parameter in config.yml when dumping assets but still create a default folder (assetic/
orimages
).write_to
parameter when generating html but use their default folder following the folder specified inframework.templating.assets_base_urls.http
01 Specifying an output directory in the config.yml
Note: You'll get exactly the same result. Neither syntax handles "output" from config (only in twig).
02 Specifying an output in the twig template
By the way I added
optimasset/
to the output path because rendering doesn't consider ````write_to``` value.dump:
render as:
Note: The rendering is correct but the dumping path is not. To fix this I had to remove the
write_to
parameter.CONCLUSION
{{ optipng(...) }}
as{{ jpegoptim(...) }}
never output the good file name.Use the syntax
{% images ... %}
.write_to
if you are using assetic to filter images {% images %} doesn't consider it when rendering html, only{% javascripts %}
and{% stylesheets %}
do.您是否已经阅读过在 Symfony2 中使用 Assetic 进行 CSS 压缩?这可能是他们正在谈论的奇怪行为,因此请跳到该页面上的第 4 点并查看提到的拉取请求 (https://github.com/symfony/symfony/pull/509)。它可能会解决你的问题:p。
And have you read Using Assetic in Symfony2 for CSS compression already? It might be the weird behavior they're talking about, so skip to point 4 on that page and look at the pull request mentioned (https://github.com/symfony/symfony/pull/509). It might solve your problem :p.