简体   繁体   English

使用php构建和重建CSS数据uri

[英]Build and Rebuild CSS data uris with php

This whole data URI nonsense is going to be grow old really quick, but until then, I'm gonna jump on the bandwagaon for at least one lap. 整个数据URI废话确实会很快变老,但是在那之前,我至少要在bandwagaon上跳一圈。

So I have CSS files that I want to update to use data URIs instead of remote location. 因此,我有一些要更新的CSS文件,以使用数据URI而不是远程位置。 I was thinking I could do this programmatically using a bit of PHP and regex and while it wouldn't be as clever and transparent as the data URI redirect idea I saw earlier, it would offer a push button "rebuild" option to fix any future files or updates. 我当时想我可以使用一些PHP和regex进行编程,尽管它不像我之前看到的数据URI重定向想法那么聪明和透明,但是它将提供一个按钮“ rebuild”选项来修复将来的问题文件或更新。 But then I ran into a mental block. 但是后来我陷入了精神障碍。 Here is the idea: 这里是想法:

  1. Fix-URI script traverses all css files stored in proper place and greps for pattern of url(".*") (with some fuzziness added later) Fix-URI脚本遍历存储在适当位置的所有css文件,并摸索为url(".*") (稍后添加一些模糊性)

  2. script confirms file is local and exists and chews contents into data uri 脚本确认文件是本地文件并且存在并将内容嚼入数据uri

  3. script replaces url found with new data uri. 脚本用新数据uri替换找到的url。

  4. When completed, script saves updated file, etc. 完成后,脚本将保存更新的文件等。

But then, of course, it occurred to me this can only work once. 但是,当然,在我看来这只能工作一次。 After that, the url would be data uri already. 之后,该URL将已经是数据uri。 I can add check for "data:" but the real dillemma for me is : how do I know the data uri is up-to-date? 我可以添加“ data:”的检查,但是对我来说真正的难题是:我怎么知道数据uri是最新的? If I replace "icon-xyz.png" to be shiny and better, not only will I have to check each data uri every time, but I will have to somehow know the original filename to compare it to, which is not a standard attribute part of the data uri as of now. 如果我将“ icon-xyz.png”替换为有光泽的更好的文件,不仅需要每次检查每个数据uri,而且还必须以某种方式知道要与之比较的原始文件名,这不是标准属性截至目前,数据uri的一部分。

What I don't want is a dynamic css file using php variables. 我不想要的是使用php变量的动态CSS文件。 I would also like to keep the solution as abstracted as possible, even if the regex horse is out the barn. 我也想使解决方案尽可能抽象,即使正则表达式马不在谷仓内。

One passing thought was to simply remove any images that have been converted or at least move them to a "converted" directory. 一个过去的想法是简单地删除所有已转换的图像,或者至少将它们移到“已转换”目录中。 This way an updated version or a new image still using plain url could be spotted right away. 这样,可以立即发现仍使用普通网址的更新版本或新图像。 Then I could datamagically convert the old already converted version of the image, grep for that and then replace where found with new file. 然后,我可以对旧的已转换版本的图像进行数据魔术转换,使用grep进行转换,然后在找到的位置替换为新文件。

I fear, however, that I'm making it more complicated than I need to and I'm missing something obvious and convenient. 但是,我担心我正在使它变得比我所需要的更加复杂,并且我缺少一些明显且方便的东西。

quick update 快速更新

Keeping it as abstracted as possible includes avoiding or at least resisting solutions such as css comments to reflect file name or commented out property above new property, etc. 使其尽可能抽象的方法包括避免或至少拒绝解决方案,例如css注释以反映文件名或在新属性上方注释掉的属性等。

My solution, dont overwrite but copy 我的解决方案,不要覆盖但要复制

css.css -> compiled/css.css css.css->编译/css.css

on change, recompile css.css to compiled/css.css 更改时,将css.css重新编译为Compiled / css.css

I do something like this on my sites compiled/v{1-9+},css1,css2.css 我在自己的网站上执行了以下操作:compiled / v {1-9 +},css1,css2.css

the webserver takes care of the generation automatically, if the css file doesn't exist it calls generatecss.php that compiles and combined the css files. Web服务器会自动处理生成,如果css文件不存在,它将调用generatecss.php来编译和合并css文件。

The version number ensures there is no caching, the second and third are combined and optimized css files created from the original css1.css and css2.css and so on. 版本号确保没有缓存,第二个和第三个结合在一起,并从原始css1.css和css2.css等创建并优化了css文件。

Keep in mind however that css files are blocking and using large css files is not always a great idea. 但是请记住,css文件被阻止,使用大的css文件并不总是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM